1# SPDX-License-Identifier: GPL-2.0-only 2# This file is part of Scapy 3# See https://scapy.net/ for more information 4# Copyright (C) 2007, 2008, 2009 Arnaud Ebalard 5# 2015, 2016 Maxence Tury 6 7""" 8TLS compression. 9""" 10 11import zlib 12 13from scapy.error import warning 14 15 16_tls_compression_algs = {} 17_tls_compression_algs_cls = {} 18 19 20class _GenericCompMetaclass(type): 21 """ 22 Compression classes are automatically registered through this metaclass. 23 """ 24 def __new__(cls, name, bases, dct): 25 the_class = super(_GenericCompMetaclass, cls).__new__(cls, name, 26 bases, dct) 27 comp_name = dct.get("name") 28 val = dct.get("val") 29 if comp_name: 30 _tls_compression_algs[val] = comp_name 31 _tls_compression_algs_cls[val] = the_class 32 return the_class 33 34 35class _GenericComp(metaclass=_GenericCompMetaclass): 36 pass 37 38 39class Comp_NULL(_GenericComp): 40 """ 41 The default and advised compression method for TLS: doing nothing. 42 """ 43 name = "null" 44 val = 0 45 46 def compress(self, s): 47 return s 48 49 def decompress(self, s): 50 return s 51 52 53class Comp_Deflate(_GenericComp): 54 """ 55 DEFLATE algorithm, specified for TLS by RFC 3749. 56 """ 57 name = "deflate" 58 val = 1 59 60 def compress(self, s): 61 tmp = self.compress_state.compress(s) 62 tmp += self.compress_state.flush(zlib.Z_FULL_FLUSH) 63 return tmp 64 65 def decompress(self, s): 66 return self.decompress_state.decompress(s) 67 68 def __init__(self): 69 self.compress_state = zlib.compressobj() 70 self.decompress_state = zlib.decompressobj() 71 72 73class Comp_LZS(_GenericComp): 74 """ 75 Lempel-Zic-Stac (LZS) algorithm, specified for TLS by RFC 3943. 76 XXX No support for now. 77 """ 78 name = "LZS" 79 val = 64 80 81 def compress(self, s): 82 warning("LZS Compression algorithm is not implemented yet") 83 return s 84 85 def decompress(self, s): 86 warning("LZS Compression algorithm is not implemented yet") 87 return s 88