-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtlv.py
More file actions
568 lines (465 loc) · 18.2 KB
/
Copy pathtlv.py
File metadata and controls
568 lines (465 loc) · 18.2 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
from __future__ import annotations
import json
import zlib
from enum import IntEnum, IntFlag
from struct import Struct, error as struct_error
from typing import ClassVar, Any, Hashable, Optional, Iterator, Sequence, \
Mapping, Dict, List
from .checksum import crc32c as crc32c_checksum
from .typing import PeerCert
__all__ = ['Type', 'SSLClient', 'ExtType', 'TLV', 'ProxyProtocolTLV',
'ProxyProtocolSSLTLV', 'ProxyProtocolExtTLV']
class Type(IntEnum):
"""The PROXY protocol TLV standard type values.
See Also:
:class:`ProxyProtocolTLV`
"""
PP2_TYPE_ALPN = 0x01
PP2_TYPE_AUTHORITY = 0x02
PP2_TYPE_CRC32C = 0x03
PP2_TYPE_NOOP = 0x04
PP2_TYPE_UNIQUE_ID = 0x05
PP2_TYPE_SSL = 0x20
PP2_TYPE_NETNS = 0x30
PP2_SUBTYPE_SSL_VERSION = 0x21
PP2_SUBTYPE_SSL_CN = 0x22
PP2_SUBTYPE_SSL_CIPHER = 0x23
PP2_SUBTYPE_SSL_SIG_ALG = 0x24
PP2_SUBTYPE_SSL_KEY_ALG = 0x25
PP2_TYPE_MIN_CUSTOM = 0xE0
PP2_TYPE_MAX_CUSTOM = 0xEF
PP2_TYPE_MIN_EXPERIMENT = 0xF0
PP2_TYPE_MAX_EXPERIMENT = 0xF7
PP2_TYPE_MIN_FUTURE = 0xF8
PP2_TYPE_MAX_FUTURE = 0xFF
class SSLClient(IntFlag):
"""The PROXY protocol ``PP2_TYPE_SSL`` client flags.
See Also:
:class:`ProxyProtocolSSLTLV`
"""
PP2_CLIENT_SSL = 0x01
PP2_CLIENT_CERT_CONN = 0x02
PP2_CLIENT_CERT_SESS = 0x04
class ExtType(IntEnum):
"""Non-standard extension TLV types.
See Also:
:class:`ProxyProtocolExtTLV`
"""
PP2_TYPE_EXT_COMPRESSION = 0x01
PP2_TYPE_EXT_SECRET_BITS = 0x02
PP2_TYPE_EXT_PEERCERT = 0x03
PP2_TYPE_EXT_DNSBL = 0x04
class TLV(Mapping[int, bytes], Hashable):
"""Defines the basic parsing and structure of a PROXY protocol TLV vector.
The unpacked TLV values are available as dict-style keys of this object,
e.g. ``tlv[0xE2]``. To serialize back to a bytestring, use ``bytes(tlv)``.
Args:
data: TLV data to parse.
init: A mapping of types to values to initialize the TLV, such as
another :class:`TLV`.
"""
__slots__ = ['_tlv', '_frozen']
_fmt = Struct('!BH')
def __init__(self, data: bytes = b'',
init: Optional[Mapping[int, bytes]] = None) -> None:
super().__init__()
self._tlv = self._unpack(data)
if init is not None:
self._tlv.update(init)
self._frozen = self._freeze()
def _freeze(self) -> Hashable:
return frozenset(self._tlv.items())
def _unpack(self, data: bytes) -> Dict[int, bytes]:
index = 0
fmt = self._fmt
results: Dict[int, bytes] = {}
while len(data) >= index + fmt.size:
type_num, size = fmt.unpack_from(data, index)
index += fmt.size
results[type_num] = bytes(data[index:index + size])
index += size
return results
def _pack(self) -> bytes:
parts: List[bytes] = []
fmt = self._fmt
tlv = self._tlv
for type_num in range(0x00, 0x100):
val = tlv.get(type_num)
if val is not None:
parts.append(fmt.pack(type_num, len(val)))
parts.append(val)
return b''.join(parts)
@property
def size(self) -> int:
"""The size of the TLV when converted to bytes."""
cur_len = 0
fmt_size = self._fmt.size
tlv = self._tlv
for type_num in range(0x00, 0x100):
val = tlv.get(type_num)
if val is not None:
cur_len += fmt_size
cur_len += len(val)
return cur_len
def __bytes__(self) -> bytes:
return self._pack()
def __getitem__(self, type_num: int) -> bytes:
return self._tlv[type_num]
def __iter__(self) -> Iterator[int]:
return iter(self._tlv)
def __bool__(self) -> bool:
return bool(self._tlv)
def __len__(self) -> int:
return len(self._tlv)
def __hash__(self) -> int:
return hash(self._frozen)
def __eq__(self, other: Any) -> bool:
if isinstance(other, type(self)):
return self._frozen == other._frozen
return super().__eq__(other)
def __repr__(self) -> str:
arg = repr(bytes(self)) if self else ''
return f'{type(self).__name__}({arg})'
class ProxyProtocolTLV(TLV):
"""Defines the TLV values that may be appended to a PROXY protocol header.
These values can provide additional information not stored in the address
data. Refer to the PROXY protocol spec for more information about each TLV.
Args:
data: TLV data to parse.
init: A mapping of types to values to initialize the TLV, such as
another :class:`TLV`.
"""
__slots__ = ['_auto_crc32c']
_crc32c_fmt = Struct('!L')
def __init__(self, data: bytes = b'',
init: Optional[Mapping[int, bytes]] = None, *,
alpn: Optional[bytes] = None,
authority: Optional[str] = None,
crc32c: Optional[int] = None,
unique_id: Optional[bytes] = None,
ssl: Optional[ProxyProtocolSSLTLV] = None,
netns: Optional[str] = None,
ext: Optional[ProxyProtocolExtTLV] = None,
auto_crc32c: bool = False) -> None:
results = dict(init or {})
if alpn is not None:
results[Type.PP2_TYPE_ALPN] = alpn
if authority is not None:
results[Type.PP2_TYPE_AUTHORITY] = authority.encode('utf-8')
if crc32c is not None:
results[Type.PP2_TYPE_CRC32C] = self._crc32c_fmt.pack(crc32c)
if unique_id is not None:
results[Type.PP2_TYPE_UNIQUE_ID] = unique_id
if ssl:
results[Type.PP2_TYPE_SSL] = bytes(ssl)
if netns is not None:
results[Type.PP2_TYPE_NETNS] = netns.encode('ascii')
if ext:
results[Type.PP2_TYPE_NOOP] = bytes(ext)
super().__init__(data, results)
self._auto_crc32c = auto_crc32c \
and crc32c is None \
and crc32c_checksum is not None
def _pack(self) -> bytes:
if self._auto_crc32c:
raise ValueError('Cannot convert to bytes with auto_crc32c=True')
return super()._pack()
@property
def _zero_crc32c(self) -> ProxyProtocolTLV:
return ProxyProtocolTLV(init=self, crc32c=0)
@property
def size(self) -> int:
if self.crc32c is None and self._auto_crc32c:
return self._zero_crc32c.size
else:
return super().size
def _compute_checksum(self, before: Sequence[bytes]) -> int:
assert crc32c_checksum is not None
crc = crc32c_checksum(b'')
for data in before:
crc = crc32c_checksum(data, crc)
return crc32c_checksum(bytes(self._zero_crc32c), crc)
def with_checksum(self, *before: bytes) -> ProxyProtocolTLV:
"""Return a copy of the current TLV values with the :attr:`.crc32c`
checksum populated according to the rules in the PROXY protocol spec.
Args:
before: The data in the PROXY protocol header before the TLV, which
is included in the checksum.
"""
if not self._auto_crc32c:
return self
crc = self._compute_checksum(before)
return ProxyProtocolTLV(init=self, crc32c=crc)
def verify_checksum(self, *before: bytes) -> bool:
"""Verifies the :attr:`.crc32c` checksum, if present, correctly matches
the expected value computed for the PROXY protocol header. If this
method returns False, the connection should likely be aborted.
Args:
before: The data in the PROXY protocol header before the TLV, which
is included in the checksum.
"""
if self.crc32c is None or crc32c_checksum is None:
return True
crc = self._compute_checksum(before)
return self.crc32c == crc
@property
def alpn(self) -> Optional[bytes]:
"""The ``PP2_TYPE_ALPN`` value."""
val = self.get(Type.PP2_TYPE_ALPN)
if val is not None:
return bytes(val)
return None
@property
def authority(self) -> Optional[str]:
"""The ``PP2_TYPE_AUTHORITY`` value."""
val = self.get(Type.PP2_TYPE_AUTHORITY)
if val is not None:
return str(val, 'utf-8')
return None
@property
def crc32c(self) -> Optional[int]:
"""The ``PP2_TYPE_CRC32C`` value."""
val = self.get(Type.PP2_TYPE_CRC32C)
if val is not None:
crc32c, = self._crc32c_fmt.unpack(val)
return int(crc32c)
return None
@property
def unique_id(self) -> bytes:
"""The ``PP2_TYPE_UNIQUE_ID`` value."""
val = self.get(Type.PP2_TYPE_UNIQUE_ID)
if val is not None:
return bytes(val)
return b''
@property
def ssl(self) -> ProxyProtocolSSLTLV:
"""The ``PP2_TYPE_SSL`` value."""
val = self.get(Type.PP2_TYPE_SSL)
if val is not None:
return ProxyProtocolSSLTLV(val)
return ProxyProtocolSSLTLV()
@property
def netns(self) -> Optional[str]:
"""The ``PP2_TYPE_NETNS`` value."""
val = self.get(Type.PP2_TYPE_NETNS)
if val is not None:
return str(val, 'ascii')
return None
@property
def ext(self) -> ProxyProtocolExtTLV:
"""The ``PP2_TYPE_NOOP`` value, possibly parsed as an extension TLV."""
val = self.get(Type.PP2_TYPE_NOOP)
if val is not None:
return ProxyProtocolExtTLV(val)
return ProxyProtocolExtTLV()
class ProxyProtocolSSLTLV(TLV):
"""The ``PP2_TYPE_SSL`` TLV, which is prefixed with a struct containing
*client* and *verify* values, then follows with ``PP2_SUBTYPE_SSL_*`` TLVs.
Args:
data: TLV data to parse.
init: A mapping of types to values to initialize the TLV, such as
another :class:`TLV`.
"""
__slots__ = ['_client', '_verify']
_prefix_fmt = Struct('!BL')
def __init__(self, data: bytes = b'',
init: Optional[Mapping[int, bytes]] = None, *,
has_ssl: Optional[bool] = None,
has_cert_conn: Optional[bool] = None,
has_cert_sess: Optional[bool] = None,
verified: Optional[bool] = None,
version: Optional[str] = None,
cn: Optional[str] = None,
cipher: Optional[str] = None,
sig_alg: Optional[str] = None,
key_alg: Optional[str] = None) -> None:
self._client = 0
self._verify = 1
results = dict(init or {})
if version is not None:
results[Type.PP2_SUBTYPE_SSL_VERSION] = version.encode('ascii')
if cn is not None:
results[Type.PP2_SUBTYPE_SSL_CN] = cn.encode('utf-8')
if cipher is not None:
results[Type.PP2_SUBTYPE_SSL_CIPHER] = cipher.encode('ascii')
if sig_alg is not None:
results[Type.PP2_SUBTYPE_SSL_SIG_ALG] = sig_alg.encode('ascii')
if key_alg is not None:
results[Type.PP2_SUBTYPE_SSL_KEY_ALG] = key_alg.encode('ascii')
super().__init__(data, results)
if has_ssl is True:
self._client |= SSLClient.PP2_CLIENT_SSL
elif has_ssl is False:
self._client &= ~SSLClient.PP2_CLIENT_SSL
if has_cert_conn is True:
self._client |= SSLClient.PP2_CLIENT_CERT_CONN
elif has_cert_conn is False:
self._client &= ~SSLClient.PP2_CLIENT_CERT_CONN
if has_cert_sess is True:
self._client |= SSLClient.PP2_CLIENT_CERT_SESS
elif has_cert_sess is False:
self._client &= ~SSLClient.PP2_CLIENT_CERT_SESS
if verified is not None:
self._verify = int(not verified)
def _unpack(self, data: bytes) -> Dict[int, bytes]:
try:
self._client, self._verify = \
self._prefix_fmt.unpack_from(data, 0)
except struct_error:
pass
return super()._unpack(data[self._prefix_fmt.size:])
def _pack(self) -> bytes:
prefix = self._prefix_fmt.pack(self.client, self.verify)
return prefix + super()._pack()
def __bool__(self) -> bool:
return super().__bool__() or bool(self.client) or self.verified
def __hash__(self) -> int:
return hash((self._frozen, self._client, self._verify))
def __eq__(self, other: Any) -> bool:
if isinstance(other, type(self)):
self_cmp = (self._frozen, self._client, self._verify)
other_cmp = (self._frozen, self._client, self._verify)
return self_cmp == other_cmp
return super().__eq__(other)
@property
def client(self) -> int:
"""The client field in the ``PP2_TYPE_SSL`` value."""
return self._client
@property
def verify(self) -> int:
"""The verify field in the ``PP2_TYPE_SSL`` value."""
return self._verify
@property
def has_ssl(self) -> bool:
"""True if the ``PP2_CLIENT_SSL`` flag was set."""
return self.client & SSLClient.PP2_CLIENT_SSL != 0
@property
def has_cert_conn(self) -> bool:
"""True if the ``PP2_CLIENT_CERT_CONN`` flag was set."""
return self.client & SSLClient.PP2_CLIENT_CERT_CONN != 0
@property
def has_cert_sess(self) -> bool:
"""True if the ``PP2_CLIENT_CERT_SESS`` flag was set."""
return self.client & SSLClient.PP2_CLIENT_CERT_SESS != 0
@property
def verified(self) -> bool:
"""True if the client provided a certificate that was successfully
verified.
"""
return self.verify == 0
@property
def version(self) -> Optional[str]:
"""The ``PP2_SUBTYPE_SSL_VERSION`` value."""
val = self.get(Type.PP2_SUBTYPE_SSL_VERSION)
if val is not None:
return str(val, 'ascii')
return None
@property
def cn(self) -> Optional[str]:
"""The ``PP2_SUBTYPE_SSL_CN`` value."""
val = self.get(Type.PP2_SUBTYPE_SSL_CN)
if val is not None:
return str(val, 'utf-8')
return None
@property
def cipher(self) -> Optional[str]:
"""The ``PP2_SUBTYPE_SSL_CIPHER`` value."""
val = self.get(Type.PP2_SUBTYPE_SSL_CIPHER)
if val is not None:
return str(val, 'ascii')
return None
@property
def sig_alg(self) -> Optional[str]:
"""The ``PP2_SUBTYPE_SSL_SIG_ALG`` value."""
val = self.get(Type.PP2_SUBTYPE_SSL_SIG_ALG)
if val is not None:
return str(val, 'ascii')
return None
@property
def key_alg(self) -> Optional[str]:
"""The ``PP2_SUBTYPE_SSL_KEY_ALG`` value."""
val = self.get(Type.PP2_SUBTYPE_SSL_KEY_ALG)
if val is not None:
return str(val, 'ascii')
return None
class ProxyProtocolExtTLV(TLV):
"""Non-standard extension TLV, which is hidden inside a
:attr:`~Type.PP2_TYPE_NOOP` and must start with :attr:`.MAGIC_PREFIX`.
Args:
data: TLV data to parse.
init: A mapping of types to values to initialize the TLV, such as
another :class:`TLV`.
"""
#: The :attr:`~Type.PP2_TYPE_NOOP` value must begin with this byte sequence
#: to be parsed as a :class:`ProxyProtocolExtTLV`.
MAGIC_PREFIX: ClassVar[bytes] = b'\x88\x1b\x79\xc1\xce\x96\x85\xb0'
_secret_bits_fmt = Struct('!H')
def __init__(self, data: bytes = b'',
init: Optional[Mapping[int, bytes]] = None, *,
compression: Optional[str] = None,
secret_bits: Optional[int] = None,
peercert: Optional[PeerCert] = None,
dnsbl: Optional[str] = None) -> None:
results = dict(init or {})
if compression is not None:
val = compression.encode('ascii')
results[ExtType.PP2_TYPE_EXT_COMPRESSION] = val
if secret_bits is not None:
val = self._secret_bits_fmt.pack(secret_bits)
results[ExtType.PP2_TYPE_EXT_SECRET_BITS] = val
if peercert is not None:
val = zlib.compress(json.dumps(peercert).encode('ascii'))
results[ExtType.PP2_TYPE_EXT_PEERCERT] = val
if dnsbl is not None:
val = dnsbl.encode('utf-8')
results[ExtType.PP2_TYPE_EXT_DNSBL] = val
super().__init__(data, results)
def _unpack(self, data: bytes) -> Dict[int, bytes]:
magic_prefix = self.MAGIC_PREFIX
magic_prefix_len = len(magic_prefix)
if data[0:magic_prefix_len] != magic_prefix:
return {}
return super()._unpack(data[magic_prefix_len:])
def _pack(self) -> bytes:
return self.MAGIC_PREFIX + super()._pack()
@property
def compression(self) -> Optional[str]:
"""The :attr:`~ExtType.PP2_TYPE_EXT_COMPRESSION` value. This is used by
the :attr:`~proxyprotocol.sock.SocketInfo.compression` value.
"""
val = self.get(ExtType.PP2_TYPE_EXT_COMPRESSION)
if val is not None:
return str(val, 'ascii')
return None
@property
def secret_bits(self) -> Optional[int]:
"""The :attr:`~ExtType.PP2_TYPE_EXT_SECRET_BITS` value. This is used to
populate the third member of the
:attr:`~proxyprotocol.sock.SocketInfo.cipher` tuple.
"""
val = self.get(ExtType.PP2_TYPE_EXT_SECRET_BITS)
if val is not None:
secret_bits, = self._secret_bits_fmt.unpack(val)
return int(secret_bits)
return None
@property
def peercert(self) -> Optional[PeerCert]:
"""The :attr:`~ExtType.PP2_TYPE_EXT_PEERCERT` value. This is used by
the :attr:`~proxyprotocol.sock.SocketInfo.peercert` value.
"""
val = self.get(ExtType.PP2_TYPE_EXT_PEERCERT)
if val is not None:
decompressed = zlib.decompress(val)
ret: PeerCert = json.loads(decompressed)
return ret
return None
@property
def dnsbl(self) -> Optional[str]:
"""The :attr:`~ExtType.PP2_TYPE_EXT_DNSBL` value. This is the hostname
or other identifier that reports a status or reputation of the
connecting IP address.
"""
val = self.get(ExtType.PP2_TYPE_EXT_DNSBL)
if val is not None:
return str(val, 'utf-8')
return None