-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path_tlsi.py
455 lines (403 loc) · 16.6 KB
/
_tlsi.py
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# SPDX-License-Identifier: MIT
# Copyright (c) 2018, Mathias Laurin
"""Interfaces defined in PEP 543 (+ DTLS)."""
from __future__ import annotations
import enum
import os
import sys
from dataclasses import dataclass, field
from typing import (
Callable,
Literal,
Mapping,
Optional,
Protocol,
Tuple,
TypeVar,
Union,
)
__all__ = ["NextProtocol", "TLSVersion", "DTLSVersion"]
if sys.version_info < (3, 9):
_Path = Union[os.PathLike, str] # type: ignore [type-arg]
else:
_Path = Union[os.PathLike[str], str]
@enum.unique
class NextProtocol(enum.Enum):
H2: bytes = b"h2"
H2C: bytes = b"h2c"
HTTP1: bytes = b"http/1.1"
WEBRTC: bytes = b"webrtc"
C_WEBRTC: bytes = b"c-webrtc"
FTP: bytes = b"ftp"
STUN: bytes = b"stun.nat-discovery"
TURN: bytes = b"stun.turn"
class MaxFragmentLength(enum.Enum):
NONE = 0
MFL_512K = 1
MFL_1024K = 2
MFL_2048K = 3
MFL_4096K = 4
class TLSVersion(enum.Enum):
# PEP 543
MINIMUM_SUPPORTED = enum.auto()
SSLv2 = enum.auto()
SSLv3 = enum.auto()
TLSv1 = enum.auto()
TLSv1_1 = enum.auto()
TLSv1_2 = enum.auto()
TLSv1_3 = enum.auto()
MAXIMUM_SUPPORTED = enum.auto()
class DTLSVersion(enum.Enum):
MINIMUM_SUPPORTED = enum.auto()
DTLSv1_0 = enum.auto()
DTLSv1_2 = enum.auto()
MAXIMUM_SUPPORTED = enum.auto()
class TrustStore(Protocol):
@classmethod
def system(cls) -> TrustStore:
"""Returns a TrustStore object that represents the system
trust database.
"""
@classmethod
def from_pem_file(cls, path: _Path) -> TrustStore:
"""Initializes a trust store from a single file full of PEMs."""
class Certificate(Protocol):
@classmethod
def from_buffer(cls, buffer: bytes) -> Certificate:
"""Creates a Certificate object from a byte buffer.
This byte buffer may be either PEM-encoded or DER-encoded. If the
buffer is PEM encoded it *must* begin with the standard PEM
preamble (a series of dashes followed by the ASCII bytes "BEGIN
CERTIFICATE" and another series of dashes). In the absence of that
preamble, the implementation may assume that the certificate is
DER-encoded instead.
"""
@classmethod
def from_file(cls, path: _Path) -> Certificate:
"""Creates a Certificate object from a file on disk.
This method may be a convenience method that wraps ``open`` and
``from_buffer``, but some TLS implementations may be able to
provide more-secure or faster methods of loading certificates that
do not involve Python code.
"""
class PrivateKey(Protocol):
@classmethod
def from_buffer(
cls,
buffer: bytes,
password: Optional[
Union[Callable[[], Union[bytes, bytearray]], bytes, bytearray]
] = None,
) -> PrivateKey:
"""Creates a PrivateKey object from a byte buffer.
This byte buffer may be either PEM-encoded or DER-encoded. If the
buffer is PEM encoded it *must* begin with the standard PEM
preamble (a series of dashes followed by the ASCII bytes "BEGIN",
the key type, and another series of dashes). In the absence of
that preamble, the implementation may assume that the certificate
is DER-encoded instead.
The key may additionally be encrypted. If it is, the ``password``
argument can be used to decrypt the key. The ``password`` argument
may be a function to call to get the password for decrypting the
private key. It will only be called if the private key is encrypted
and a password is necessary. It will be called with no arguments,
and it should return either bytes or bytearray containing the
password. Alternatively a bytes, or bytearray value may be supplied
directly as the password argument. It will be ignored if the
private key is not encrypted and no password is needed.
"""
@classmethod
def from_file(
cls,
path: _Path,
password: Optional[
Union[Callable[[], Union[bytes, bytearray]], bytes, bytearray]
] = None,
) -> PrivateKey:
"""Creates a PrivateKey object from a file on disk.
This method may be a convenience method that wraps ``open`` and
``from_buffer``, but some TLS implementations may be able to
provide more-secure or faster methods of loading certificates that
do not involve Python code.
The ``password`` parameter behaves exactly as the equivalent
parameter on ``from_buffer``.
"""
CipherSuite = object
DEFAULT_CIPHER_LIST = ()
ServerNameCallback = object
class __DefaultValue(enum.Enum):
DEFAULT_VALUE = enum.auto()
_DEFAULT_VALUE = __DefaultValue.DEFAULT_VALUE
T = TypeVar("T")
_Wrap = Union[T, Literal[__DefaultValue.DEFAULT_VALUE]]
def _unwrap(x: _Wrap[T], default: T) -> T:
if x is _DEFAULT_VALUE:
return default
return x
_CertificateChain = Tuple[Tuple[Certificate, ...], PrivateKey]
_Ciphers = Tuple[Union[CipherSuite, int], ...]
_InnerProtocols = Tuple[Union[NextProtocol, bytes], ...]
@dataclass(frozen=True)
class TLSConfiguration:
# pylint: disable=too-many-instance-attributes, too-many-arguments
validate_certificates: bool = True
certificate_chain: Optional[_CertificateChain] = None
ciphers: Optional[_Ciphers] = None
inner_protocols: Optional[_InnerProtocols] = None
lowest_supported_version: TLSVersion = TLSVersion.TLSv1
highest_supported_version: TLSVersion = TLSVersion.MAXIMUM_SUPPORTED
trust_store: Optional[TrustStore] = None
max_fragmentation_length: Optional[MaxFragmentLength] = None
read_timeout: float = 0.0
sni_callback: Optional[ServerNameCallback] = None
pre_shared_key: Optional[Tuple[str, bytes]] = None
pre_shared_key_store: Mapping[str, bytes] = field(default_factory=dict)
def __eq__(self, other: object) -> bool:
if not isinstance(other, TLSConfiguration):
return NotImplemented
return all(
(
self.validate_certificates == other.validate_certificates,
self.certificate_chain == other.certificate_chain
or (
not self.certificate_chain and not other.certificate_chain
),
self.ciphers == other.ciphers
or (not self.ciphers and not other.ciphers),
self.inner_protocols == other.inner_protocols
or (not self.inner_protocols and not other.inner_protocols),
self.lowest_supported_version == other.lowest_supported_version
or (
self.lowest_supported_version
in {
TLSVersion.MINIMUM_SUPPORTED,
TLSVersion.MAXIMUM_SUPPORTED,
}
)
or (
other.lowest_supported_version
in {
TLSVersion.MINIMUM_SUPPORTED,
TLSVersion.MAXIMUM_SUPPORTED,
}
),
self.highest_supported_version
== other.highest_supported_version
or (
self.highest_supported_version
in {
TLSVersion.MINIMUM_SUPPORTED,
TLSVersion.MAXIMUM_SUPPORTED,
}
)
or (
other.highest_supported_version
in {
TLSVersion.MINIMUM_SUPPORTED,
TLSVersion.MAXIMUM_SUPPORTED,
}
),
self.trust_store == other.trust_store
or (not self.trust_store and not other.trust_store),
self.max_fragmentation_length
== other.max_fragmentation_length,
self.sni_callback == other.sni_callback,
self.pre_shared_key == other.pre_shared_key,
self.pre_shared_key_store == other.pre_shared_key_store,
self.read_timeout == other.read_timeout,
)
)
def update(
self,
validate_certificates: _Wrap[bool] = _DEFAULT_VALUE,
certificate_chain: _Wrap[_CertificateChain] = _DEFAULT_VALUE,
ciphers: _Wrap[_Ciphers] = _DEFAULT_VALUE,
inner_protocols: _Wrap[_InnerProtocols] = _DEFAULT_VALUE,
lowest_supported_version: _Wrap[TLSVersion] = _DEFAULT_VALUE,
highest_supported_version: _Wrap[TLSVersion] = _DEFAULT_VALUE,
trust_store: _Wrap[TrustStore] = _DEFAULT_VALUE,
max_fragmentation_length: _Wrap[MaxFragmentLength] = _DEFAULT_VALUE,
read_timeout: _Wrap[float] = _DEFAULT_VALUE,
sni_callback: _Wrap[Optional[ServerNameCallback]] = _DEFAULT_VALUE,
pre_shared_key: _Wrap[Tuple[str, bytes]] = _DEFAULT_VALUE,
pre_shared_key_store: _Wrap[Mapping[str, bytes]] = _DEFAULT_VALUE,
) -> TLSConfiguration:
"""
Create a new ``TLSConfiguration``, overriding some of the settings
on the original configuration with the new settings.
"""
return self.__class__(
validate_certificates=_unwrap(
validate_certificates,
self.validate_certificates,
),
certificate_chain=_unwrap(
certificate_chain,
self.certificate_chain,
),
ciphers=_unwrap(ciphers, self.ciphers),
inner_protocols=_unwrap(inner_protocols, self.inner_protocols),
lowest_supported_version=_unwrap(
lowest_supported_version,
self.lowest_supported_version,
),
highest_supported_version=_unwrap(
highest_supported_version,
self.highest_supported_version,
),
trust_store=_unwrap(trust_store, self.trust_store),
max_fragmentation_length=_unwrap(
max_fragmentation_length,
self.max_fragmentation_length,
),
sni_callback=_unwrap(sni_callback, self.sni_callback),
read_timeout=_unwrap(read_timeout, self.read_timeout),
pre_shared_key=_unwrap(pre_shared_key, self.pre_shared_key),
pre_shared_key_store=_unwrap(
pre_shared_key_store,
self.pre_shared_key_store,
),
)
@dataclass(frozen=True)
class DTLSConfiguration:
# pylint: disable=too-many-instance-attributes, too-many-arguments
validate_certificates: bool = True
certificate_chain: Optional[_CertificateChain] = None
ciphers: Optional[_Ciphers] = None
inner_protocols: Optional[_InnerProtocols] = None
lowest_supported_version: DTLSVersion = DTLSVersion.DTLSv1_0
highest_supported_version: DTLSVersion = DTLSVersion.MAXIMUM_SUPPORTED
trust_store: Optional[TrustStore] = None
max_fragmentation_length: Optional[MaxFragmentLength] = None
anti_replay: bool = True
handshake_timeout_min: float = 1.0
handshake_timeout_max: float = 60.0
read_timeout: float = 0.0
sni_callback: Optional[ServerNameCallback] = None
pre_shared_key: Optional[Tuple[str, bytes]] = None
pre_shared_key_store: Mapping[str, bytes] = field(default_factory=dict)
def __eq__(self, other: object) -> bool:
if not isinstance(other, DTLSConfiguration):
return NotImplemented
return all(
(
self.validate_certificates == other.validate_certificates,
self.certificate_chain == other.certificate_chain
or (
not self.certificate_chain and not other.certificate_chain
),
self.ciphers == other.ciphers
or (not self.ciphers and not other.ciphers),
self.inner_protocols == other.inner_protocols
or (not self.inner_protocols and not other.inner_protocols),
self.lowest_supported_version == other.lowest_supported_version
or (
self.lowest_supported_version
in {
DTLSVersion.MINIMUM_SUPPORTED,
DTLSVersion.MAXIMUM_SUPPORTED,
}
)
or (
other.lowest_supported_version
in {
DTLSVersion.MINIMUM_SUPPORTED,
DTLSVersion.MAXIMUM_SUPPORTED,
}
),
self.highest_supported_version
== other.highest_supported_version
or (
self.highest_supported_version
in {
DTLSVersion.MINIMUM_SUPPORTED,
DTLSVersion.MAXIMUM_SUPPORTED,
}
)
or (
other.highest_supported_version
in {
DTLSVersion.MINIMUM_SUPPORTED,
DTLSVersion.MAXIMUM_SUPPORTED,
}
),
self.trust_store == other.trust_store
or (not self.trust_store and not other.trust_store)
or (not self.trust_store and not other.trust_store),
self.max_fragmentation_length
== other.max_fragmentation_length,
self.anti_replay == other.anti_replay,
self.handshake_timeout_min == other.handshake_timeout_min,
self.handshake_timeout_max == other.handshake_timeout_max,
self.read_timeout == other.read_timeout,
self.sni_callback == other.sni_callback,
self.pre_shared_key == other.pre_shared_key,
self.pre_shared_key_store == other.pre_shared_key_store,
)
)
def update(
self,
validate_certificates: _Wrap[bool] = _DEFAULT_VALUE,
certificate_chain: _Wrap[_CertificateChain] = _DEFAULT_VALUE,
ciphers: _Wrap[_Ciphers] = _DEFAULT_VALUE,
inner_protocols: _Wrap[_InnerProtocols] = _DEFAULT_VALUE,
lowest_supported_version: _Wrap[DTLSVersion] = _DEFAULT_VALUE,
highest_supported_version: _Wrap[DTLSVersion] = _DEFAULT_VALUE,
trust_store: _Wrap[TrustStore] = _DEFAULT_VALUE,
max_fragmentation_length: _Wrap[MaxFragmentLength] = _DEFAULT_VALUE,
anti_replay: _Wrap[bool] = _DEFAULT_VALUE,
handshake_timeout_min: _Wrap[float] = _DEFAULT_VALUE,
handshake_timeout_max: _Wrap[float] = _DEFAULT_VALUE,
read_timeout: _Wrap[float] = _DEFAULT_VALUE,
sni_callback: _Wrap[ServerNameCallback] = _DEFAULT_VALUE,
pre_shared_key: _Wrap[Tuple[str, bytes]] = _DEFAULT_VALUE,
pre_shared_key_store: _Wrap[Mapping[str, bytes]] = _DEFAULT_VALUE,
) -> DTLSConfiguration:
"""
Create a new ``TLSConfiguration``, overriding some of the settings
on the original configuration with the new settings.
"""
return self.__class__(
validate_certificates=_unwrap(
validate_certificates,
self.validate_certificates,
),
certificate_chain=_unwrap(
certificate_chain,
self.certificate_chain,
),
ciphers=_unwrap(ciphers, self.ciphers),
inner_protocols=_unwrap(inner_protocols, self.inner_protocols),
lowest_supported_version=_unwrap(
lowest_supported_version,
self.lowest_supported_version,
),
highest_supported_version=_unwrap(
highest_supported_version,
self.highest_supported_version,
),
trust_store=_unwrap(trust_store, self.trust_store),
max_fragmentation_length=_unwrap(
max_fragmentation_length,
self.max_fragmentation_length,
),
anti_replay=_unwrap(anti_replay, self.anti_replay),
handshake_timeout_min=_unwrap(
handshake_timeout_min,
self.handshake_timeout_min,
),
handshake_timeout_max=_unwrap(
handshake_timeout_max,
self.handshake_timeout_max,
),
read_timeout=_unwrap(
read_timeout,
self.read_timeout,
),
sni_callback=_unwrap(sni_callback, self.sni_callback),
pre_shared_key=_unwrap(pre_shared_key, self.pre_shared_key),
pre_shared_key_store=_unwrap(
pre_shared_key_store,
self.pre_shared_key_store,
),
)