summaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py3/twisted/protocols/_sni.py
blob: ce8b5bf9ffbf2c34caf2683898bb91f258939f9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# -*- test-case-name: twisted.internet.test.test_endpoints -*-

from __future__ import annotations

from dataclasses import dataclass
from functools import cached_property, partial
from typing import Callable

from zope.interface import implementer

from OpenSSL.crypto import FILETYPE_PEM
from OpenSSL.SSL import Connection, Context

from cryptography.x509 import DNSName, ExtensionOID, load_pem_x509_certificate

from twisted.internet.defer import Deferred
from twisted.internet.interfaces import (
    IListeningPort,
    IOpenSSLServerConnectionCreator,
    IProtocolFactory,
    IReactorTime,
    IStreamServerEndpoint,
)
from twisted.internet.ssl import (
    DN,
    Certificate,
    CertificateOptions,
    KeyPair,
    PrivateCertificate,
)
from twisted.logger import Logger
from twisted.protocols._tls_legacy import SomeConnectionCreator
from twisted.protocols.tls import TLSMemoryBIOFactory, TLSMemoryBIOProtocol
from twisted.python.filepath import FilePath

log = Logger()


def lookupWithWildcard(
    flatLookup: Callable[[bytes | None], Context | None], name: bytes | None
) -> Context | None:
    """
    Look up an OpenSSL context for the given domain name, or construct a
    default one suitable for bootstrapping the connection.
    """
    candidate = flatLookup(name)
    if candidate is None:
        if name is not None:
            segments = name.split(b".")
            segments[0] = b"*"
            wildcardName = b".".join(segments)
            candidate = flatLookup(wildcardName)
    if candidate is None:
        log.warn("no server certificate for name {name!r}", name=name)
    return candidate


@implementer(IOpenSSLServerConnectionCreator)
@dataclass
class SNIConnectionCreator:
    """
    (Private) L{IOpenSSLServerConnectionCreator} implementation that creates an
    OpenSSL connection with a context that will switch to the appropriate one.
    """

    _contextLookup: Callable[[bytes | None], Context | None]
    """
    This method should look up an OpenSSL Context object for the given DNS
    name, or one that is suitable for unidentified clients.  The lookup may
    fail and return None.
    """

    @cached_property
    def defaultContext(self) -> Context:
        """
        Create and cache the OpenSSL context that connections will initially be
        using.  This constructs a default context which doesn't know its domain
        name by delegating to C{self._contextLookup} with None, then sets the
        TLS extension servername callback to get invoked to I{switch} contexts
        by doing another lookup when the client sends its servername.

        @note: The client I{might} never send a servername at all, in which
            case it will be stuck.  This edge case is not handled particularly
            well right now.  Handling it better would involve some changes in
            this code (to hook the handshake completion callback rather than
            just the servername callback) as well as better ability to
            customize which certificate produces the default context in the
            implementation of C{_contextLookup}, which is to say, mostly
            L{PEMObjects}.
        """
        lookedUp = lookupWithWildcard(self._contextLookup, None)
        if lookedUp is None:
            blankOptions = CertificateOptions(
                contextForServerName=partial(
                    lookupWithWildcard,
                    self._contextLookup,
                )
            )
            return blankOptions.getContext()

        return lookedUp

    def serverConnectionForTLS(self, protocol: TLSMemoryBIOProtocol) -> Connection:
        """
        Construct an OpenSSL server connection that can react to the TLS
        servername callback to select an appropriate certificate based on a
        mapping.

        @param protocol: The protocol initiating a TLS connection.

        @return: a newly-created connection.
        """
        return Connection(self.defaultContext)


@implementer(IStreamServerEndpoint)
class TLSServerEndpoint:
    """
    A wrapper L{IStreamServerEndpoint} that can run TLS over an arbitrary other
    L{IStreamServerEndpoint} (most commonly, TCP).
    """

    def __init__(
        self,
        endpoint: IStreamServerEndpoint,
        connectionCreator: SomeConnectionCreator,
        clock: IReactorTime | None = None,
    ) -> None:
        """
        @param endpoint: the endpoint to run over.

        @param connectionCreator: The object that will construct OpenSSL
            connections (or Contexts).

        @param clock: The clock which will be used to schedule buffer flushes.
        """
        self.endpoint = endpoint
        self.connectionCreator = connectionCreator
        self.clock = clock

    def listen(self, factory: IProtocolFactory) -> Deferred[IListeningPort]:
        """
        Begin listening with the given factory.
        """
        return self.endpoint.listen(
            TLSMemoryBIOFactory(
                self.connectionCreator, False, factory, clock=self.clock
            )
        )


def _getSubjectAltNames(c: Certificate) -> list[str]:
    """
    Get all the DNSName SANs for a given certificate.
    """
    return [
        value
        for extension in load_pem_x509_certificate(c.dumpPEM()).extensions
        if extension.oid == ExtensionOID.SUBJECT_ALTERNATIVE_NAME
        for value in extension.value.get_values_for_type(DNSName)
    ]


def autoReloadingDirectoryOfPEMs(
    path: FilePath[str],
) -> Callable[[bytes | None], Context | None]:
    """
    Construct a callable that can look up a HTTPS certificate based on their
    DNS names, by inspecting a directory full of PEM objects.  When
    encountering a lookup failure, the directory will be reloaded, so that if
    new certificates are added they will be picked up.
    """
    # TODO: some flaws with this approach

    """
        1. too much re-scanning; re-reading full file contents for every single
           certificate even if only one has changed.  a mtime/length cache
           would be a good place to start with this

        2. too trusting; we get a network request for a billion certificate
           names per second, we go ahead and do a bunch of work every single
           time (and, see point 1, re-scan and re-parse every single file)

        3. not *enough* re-scanning on the happy path; if certificates go
           stale, we just let them sit there until we get an unknown hostname

        4. we don't look at notAfter/notBefore, so if we find multiple certs,
           we may end up using the wrong one

    """

    certMap: dict[str, CertificateOptions]

    def doReload() -> None:
        nonlocal certMap
        certMap = PEMObjects.fromDirectory(path).inferDomainMapping()

    def lookup(name: bytes | None, shouldReload: bool = True) -> Context | None:
        name = next(iter(certMap.keys()), "").encode() if name in (None, b"") else name
        assert name is not None
        if (options := certMap.get(name.decode())) is not None:
            return options.getContext()
        if not shouldReload:
            return None
        msg = "could not find domain {name}, re-loading {path}"
        log.warn(msg, name=name, path=path)
        doReload()
        return lookup(name, False)

    doReload()
    return lookup


@dataclass
class PEMObjects:
    """
    A collection of objects loaded from a collection of PEM-encoded files.
    """

    _certificates: list[tuple[FilePath[str], Certificate]]
    """
    A list of pairs of (FilePath, Certificate) that indicates what files
    contain what certificates.
    """
    _keyPairs: list[tuple[FilePath[str], KeyPair]]
    """
    A list of pairs of (FilePath, KeyPair) that indicates what pairs contain
    what certificates.
    """

    @classmethod
    def fromDirectory(cls, directory: FilePath[str]) -> PEMObjects:
        """
        Walk through the given directory looking for files with a `.pem`
        extension, and instantiate a L{PEMObjects} containing all certificates
        and key pairs from those files.

        @param directory: a L{FilePath} pointing at a directory in the
            filesystem which may contain some PEM files.
        """
        self = PEMObjects([], [])
        for fp in directory.walk():
            if fp.basename().endswith(".pem") and fp.isfile():
                subself = cls.fromFile(fp)
                self._certificates.extend(subself._certificates)
                self._keyPairs.extend(subself._keyPairs)
        return self

    @classmethod
    def fromFile(cls, fp: FilePath[str]) -> PEMObjects:
        """
        Load some objects from the lines of a single PEM file.

        @param fp: A L{FilePath} pointing at a file on the filesystem whose
            contents should be PEM data.
        """
        certBlobs: list[bytes] = []
        keyBlobs: list[bytes] = []
        blobs = [b""]
        with fp.open() as pemlines:
            for line in pemlines:
                if line.startswith(b"-----BEGIN"):
                    blobs = certBlobs if b"CERTIFICATE" in line else keyBlobs
                    blobs.append(b"")
                blobs[-1] += line
        return cls(
            _keyPairs=[
                (fp, KeyPair.load(keyBlob, FILETYPE_PEM)) for keyBlob in keyBlobs
            ],
            _certificates=[
                (fp, Certificate.loadPEM(certBlob)) for certBlob in certBlobs
            ],
        )

    def inferDomainMapping(self) -> dict[str, CertificateOptions]:
        """
        Return a mapping of DNS name to L{CertificateOptions}.
        """

        privateCerts = []

        certificatesByFingerprint = {
            certificate.getPublicKey().keyHash(): certificate
            for (_, certificate) in self._certificates
        }

        for pairPath, keyPair in self._keyPairs:
            keyHash = keyPair.keyHash()
            matchingCertificate = certificatesByFingerprint.pop(keyHash, None)
            if matchingCertificate is None:
                # log something?
                log.warn(
                    "unused private key at {path} with hash {hash}",
                    path=pairPath.path,
                    hash=keyHash,
                )
                continue
            privateCerts.append(
                (
                    _getSubjectAltNames(matchingCertificate),
                    PrivateCertificate.fromCertificateAndKeyPair(
                        matchingCertificate, keyPair
                    ),
                )
            )

        noPrivateKeys = [
            Certificate.load(dumped)
            for dumped in {each.dump() for each in certificatesByFingerprint.values()}
        ]

        def hashDN(dn: DN) -> tuple[tuple[str, bytes], ...]:
            return tuple(sorted(dn.items()))

        bySubject = {
            hashDN(eachIntermediate.getSubject()): eachIntermediate
            for eachIntermediate in noPrivateKeys
        }

        result: dict[str, CertificateOptions] = {}

        def flatLookup(servername: bytes | None) -> Context | None:
            if servername is None:
                return None
            options = result.get(servername.decode("ascii"))
            if options is not None:
                return options.getContext()
            return None

        def nameToContext(servername: bytes | None) -> Context | None:
            return lookupWithWildcard(flatLookup, servername)

        for names, privateCert in privateCerts:
            chain = []
            chained = privateCert
            while hashDN(chained.getIssuer()) in bySubject:
                chained = bySubject[hashDN(chained.getIssuer())]
                chain.append(chained.original)
            options = CertificateOptions(
                certificate=privateCert.original,
                privateKey=privateCert.privateKey.original,
                extraCertChain=chain,
                contextForServerName=nameToContext,
            )
            for dnsName in names:
                result[dnsName] = options
        return result