<s> r'''<STR_LIT>''' <EOL> from nogotofail . mitm . util . tls . types import parse , Version , Random , CompressionMethod <EOL> from nogotofail . mitm . util . ssl2 . types import Cipher <EOL> import struct <EOL> class ClientHello ( object ) : <EOL> def __init__ ( <EOL> self , version , random , session_id , ciphers ) : <EOL> self . version = version <EOL> self . random = random <EOL> self . session_id = session_id <EOL> self . ciphers = ciphers <EOL> self . compression_method = [ CompressionMethod ( <NUM_LIT:0> ) ] <EOL> self . extension_list = [ ] <EOL> self . extensions = { } <EOL> def __str__ ( self ) : <EOL> ciphers = "<STR_LIT:\n>" . join ( map ( lambda s : "<STR_LIT:\t>" + str ( s ) , self . ciphers ) ) <EOL> session_id = "<STR_LIT>" <EOL> return "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" % ( self . version , self . random , self . session_id , ciphers ) <EOL> @ staticmethod <EOL> def from_stream ( body ) : <EOL> """<STR_LIT>""" <EOL> version , index = Version . from_stream ( body ) <EOL> ciphers_length , sessionid_length , random_length = struct . unpack_from ( <EOL> "<STR_LIT>" , body , index ) <EOL> index += <NUM_LIT:6> ; <EOL> if ciphers_length % <NUM_LIT:3> != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if ciphers_length + sessionid_length + random_length != len ( body ) - index : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if random_length < <NUM_LIT:16> or random_length > <NUM_LIT:32> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> ciphers , read_amt = ( <EOL> parse . parse_tls_list ( <EOL> body [ index : ] , ciphers_length , <EOL> Cipher . from_stream ) ) <EOL> index += read_amt <EOL> session_id , read_amt = ( <EOL> parse . parse_tls_list ( <EOL> body [ index : ] , sessionid_length , <EOL> parse . parse_opaque ) ) <EOL> index += read_amt <EOL> random = body [ index : index + random_length ] <EOL> index += random_length <EOL> return ClientHello ( <EOL> version , random , session_id , ciphers ) , index <EOL> typemap = { <EOL> <NUM_LIT:1> : ClientHello , <EOL> } <EOL> class HandshakeMessage ( object ) : <EOL> def __init__ ( self , type , obj ) : <EOL> self . obj = obj <EOL> self . type = type <EOL> @ staticmethod <EOL> def from_stream ( body ) : <EOL> if len ( body ) < <NUM_LIT:3> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if ord ( body [ <NUM_LIT:0> ] ) & <NUM_LIT> == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> length , msg_type = struct . unpack_from ( "<STR_LIT>" , body , <NUM_LIT:0> ) <EOL> length &= <NUM_LIT> <EOL> msg_data = body [ <NUM_LIT:3> : <NUM_LIT:2> + length ] <EOL> if len ( msg_data ) != length - <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> type = typemap . get ( msg_type ) <EOL> if not type : <EOL> raise ValueError ( "<STR_LIT>" % msg_type ) <EOL> obj , size = type . from_stream ( msg_data ) <EOL> if len ( msg_data ) != size : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return HandshakeMessage ( msg_type , obj ) , <NUM_LIT:2> + length </s>
