1## This file is part of Scapy 2## Copyright (C) 2007, 2008, 2009 Arnaud Ebalard 3## 2015, 2016 Maxence Tury 4## This program is published under a GPLv2 license 5 6""" 7TLS compression. 8""" 9 10from __future__ import absolute_import 11import zlib 12 13from scapy.error import warning 14import scapy.modules.six as six 15 16 17_tls_compression_algs = {} 18_tls_compression_algs_cls = {} 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(six.with_metaclass(_GenericCompMetaclass, object)): 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 52class Comp_Deflate(_GenericComp): 53 """ 54 DEFLATE algorithm, specified for TLS by RFC 3749. 55 """ 56 name = "deflate" 57 val = 1 58 59 def compress(self, s): 60 tmp = self.compress_state.compress(s) 61 tmp += self.compress_state.flush(zlib.Z_FULL_FLUSH) 62 return tmp 63 64 def decompress(self, s): 65 return self.decompress_state.decompress(s) 66 67 def __init__(self): 68 self.compress_state = zlib.compressobj() 69 self.decompress_state = zlib.decompressobj() 70 71class Comp_LZS(_GenericComp): 72 """ 73 Lempel-Zic-Stac (LZS) algorithm, specified for TLS by RFC 3943. 74 XXX No support for now. 75 """ 76 name = "LZS" 77 val = 64 78 79 def compress(self, s): 80 warning("LZS Compression algorithm is not implemented yet") 81 return s 82 83 def decompress(self, s): 84 warning("LZS Compression algorithm is not implemented yet") 85 return s 86 87