<s> import six <EOL> import sys <EOL> from ctypes import * <EOL> from ctypes . util import find_library <EOL> if sys . platform == '<STR_LIT:win32>' or sys . platform == '<STR_LIT>' : <EOL> _eay = CDLL ( '<STR_LIT>' ) <EOL> else : <EOL> _eay = CDLL ( find_library ( '<STR_LIT>' ) ) <EOL> ERR_get_error = _eay . ERR_get_error <EOL> ERR_get_error . argtypes = [ ] <EOL> ERR_get_error . restype = c_ulong <EOL> ERR_error_string_n = _eay . ERR_error_string_n <EOL> ERR_error_string_n . argtypes = [ c_ulong , c_char_p , c_size_t ] <EOL> ERR_error_string_n . restype = None <EOL> class SSLError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message , * args ) : <EOL> message = message % args <EOL> err = ERR_get_error ( ) <EOL> if err : <EOL> message += '<STR_LIT::>' <EOL> while err : <EOL> buf = create_string_buffer ( <NUM_LIT> ) <EOL> ERR_error_string_n ( err , buf , <NUM_LIT> ) <EOL> message += '<STR_LIT>' % string_at ( buf , <NUM_LIT> ) <EOL> err = ERR_get_error ( ) <EOL> super ( SSLError , self ) . __init__ ( message ) <EOL> BIO_new = _eay . BIO_new <EOL> BIO_new . argtypes = [ c_void_p ] <EOL> BIO_new . restype = c_void_p <EOL> BIO_new_mem_buf = _eay . BIO_new_mem_buf <EOL> BIO_new_mem_buf . argtypes = [ c_void_p , c_int ] <EOL> BIO_new_mem_buf . restype = c_void_p <EOL> BIO_s_mem = _eay . BIO_s_mem <EOL> BIO_s_mem . argtypes = [ ] <EOL> BIO_s_mem . restype = c_void_p <EOL> BIO_ctrl = _eay . BIO_ctrl <EOL> BIO_ctrl . argtypes = [ c_void_p , c_int , c_long , c_void_p ] <EOL> BIO_ctrl . restype = c_long <EOL> BIO_CTRL_RESET = <NUM_LIT:1> <EOL> BIO_CTRL_INFO = <NUM_LIT:3> <EOL> def BIO_reset ( b ) : <EOL> return BIO_ctrl ( b , BIO_CTRL_RESET , <NUM_LIT:0> , None ) <EOL> def BIO_get_mem_data ( b , pp ) : <EOL> return BIO_ctrl ( b , BIO_CTRL_INFO , <NUM_LIT:0> , pp ) <EOL> BIO_free = _eay . BIO_free <EOL> BIO_free . argtypes = [ c_void_p ] <EOL> BIO_free . restype = c_int <EOL> def BIO_free_errcheck ( result , func , arguments ) : <EOL> if result == <NUM_LIT:0> : <EOL> raise SSLError ( '<STR_LIT>' ) <EOL> BIO_free . errcheck = BIO_free_errcheck <EOL> PEM_read_bio_RSAPrivateKey = _eay . PEM_read_bio_RSAPrivateKey <EOL> PEM_read_bio_RSAPrivateKey . argtypes = [ c_void_p , c_void_p , c_void_p , c_void_p ] <EOL> PEM_read_bio_RSAPrivateKey . restype = c_void_p <EOL> PEM_read_bio_RSAPublicKey = _eay . PEM_read_bio_RSAPublicKey <EOL> PEM_read_bio_RSAPublicKey . argtypes = [ c_void_p , c_void_p , c_void_p , c_void_p ] <EOL> PEM_read_bio_RSAPublicKey . restype = c_void_p <EOL> PEM_write_bio_RSAPrivateKey = _eay . PEM_write_bio_RSAPrivateKey <EOL> PEM_write_bio_RSAPrivateKey . argtypes = [ c_void_p , c_void_p , c_void_p , c_char_p , c_int , c_void_p , c_void_p ] <EOL> PEM_write_bio_RSAPrivateKey . restype = c_int <EOL> PEM_write_bio_RSAPublicKey = _eay . PEM_write_bio_RSAPublicKey <EOL> PEM_write_bio_RSAPublicKey . argtypes = [ c_void_p , c_void_p ] <EOL> PEM_write_bio_RSAPublicKey . restype = c_int <EOL> RSA_private_encrypt = _eay . RSA_private_encrypt <EOL> RSA_private_encrypt . argtypes = [ c_int , c_void_p , c_void_p , c_void_p , c_int ] <EOL> RSA_private_encrypt . restype = c_int <EOL> RSA_public_decrypt = _eay . RSA_public_decrypt <EOL> RSA_public_decrypt . argtypes = [ c_int , c_void_p , c_void_p , c_void_p , c_int ] <EOL> RSA_public_decrypt . restype = c_int <EOL> RSA_PKCS1_PADDING = <NUM_LIT:1> <EOL> RSA_NO_PADDING = <NUM_LIT:3> <EOL> RSA_size = _eay . RSA_size <EOL> RSA_size . argtypes = [ c_void_p ] <EOL> RSA_size . restype = c_int <EOL> RSA_generate_key = _eay . RSA_generate_key <EOL> RSA_generate_key . argtypes = [ c_int , c_ulong , c_void_p , c_void_p ] <EOL> RSA_generate_key . restype = c_void_p <EOL> RSA_F4 = <NUM_LIT> <EOL> RSA_free = _eay . RSA_free <EOL> RSA_free . argtypes = [ c_void_p ] <EOL> class Key ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fp = None ) : <EOL> self . key = None <EOL> self . public = False <EOL> if not fp : <EOL> return <EOL> if isinstance ( fp , six . binary_type ) and fp . startswith ( b'<STR_LIT>' ) : <EOL> self . raw = fp <EOL> elif isinstance ( fp , six . string_types ) : <EOL> self . raw = open ( fp , '<STR_LIT:rb>' ) . read ( ) <EOL> else : <EOL> self . raw = fp . read ( ) <EOL> self . _load_key ( ) <EOL> def _load_key ( self ) : <EOL> if b'<STR_LIT>' in self . raw : <EOL> buf = create_string_buffer ( self . raw , len ( self . raw ) ) <EOL> else : <EOL> buf = create_string_buffer ( self . raw ) <EOL> bio = BIO_new_mem_buf ( buf , len ( buf ) ) <EOL> try : <EOL> self . key = PEM_read_bio_RSAPrivateKey ( bio , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> if not self . key : <EOL> BIO_reset ( bio ) <EOL> self . public = True <EOL> self . key = PEM_read_bio_RSAPublicKey ( bio , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> if not self . key : <EOL> raise SSLError ( '<STR_LIT>' ) <EOL> finally : <EOL> BIO_free ( bio ) <EOL> @ classmethod <EOL> def generate ( cls , size = <NUM_LIT> , exp = RSA_F4 ) : <EOL> self = cls ( ) <EOL> self . key = RSA_generate_key ( size , exp , None , None ) <EOL> return self <EOL> def private_encrypt ( self , value , padding = RSA_PKCS1_PADDING ) : <EOL> if self . public : <EOL> raise SSLError ( '<STR_LIT>' ) <EOL> if six . PY3 and not isinstance ( value , bytes ) : <EOL> buf = create_string_buffer ( value . encode ( ) , len ( value ) ) <EOL> else : <EOL> buf = create_string_buffer ( value , len ( value ) ) <EOL> size = RSA_size ( self . key ) <EOL> output = create_string_buffer ( size ) <EOL> ret = RSA_private_encrypt ( len ( buf ) , buf , output , self . key , padding ) <EOL> if ret <= <NUM_LIT:0> : <EOL> raise SSLError ( '<STR_LIT>' ) <EOL> return output . raw [ : ret ] <EOL> def public_decrypt ( self , value , padding = RSA_PKCS1_PADDING ) : <EOL> if six . PY3 and not isinstance ( value , bytes ) : <EOL> buf = create_string_buffer ( value . encode ( ) , len ( value ) ) <EOL> else : <EOL> buf = create_string_buffer ( value , len ( value ) ) <EOL> size = RSA_size ( self . key ) <EOL> output = create_string_buffer ( size ) <EOL> ret = RSA_public_decrypt ( len ( buf ) , buf , output , self . key , padding ) <EOL> if ret <= <NUM_LIT:0> : <EOL> raise SSLError ( '<STR_LIT>' ) <EOL> if six . PY3 and isinstance ( output . raw , bytes ) : <EOL> return output . raw [ : ret ] . decode ( ) <EOL> else : <EOL> return output . raw [ : ret ] <EOL> def private_export ( self ) : <EOL> if self . public : <EOL> raise SSLError ( '<STR_LIT>' ) <EOL> out = BIO_new ( BIO_s_mem ( ) ) <EOL> PEM_write_bio_RSAPrivateKey ( out , self . key , None , None , <NUM_LIT:0> , None , None ) <EOL> buf = c_char_p ( ) <EOL> count = BIO_get_mem_data ( out , byref ( buf ) ) <EOL> pem = string_at ( buf , count ) <EOL> BIO_free ( out ) <EOL> return pem <EOL> def public_export ( self ) : <EOL> out = BIO_new ( BIO_s_mem ( ) ) <EOL> PEM_write_bio_RSAPublicKey ( out , self . key ) <EOL> buf = c_char_p ( ) <EOL> count = BIO_get_mem_data ( out , byref ( buf ) ) <EOL> pem = string_at ( buf , count ) <EOL> BIO_free ( out ) <EOL> return pem <EOL> def __del__ ( self ) : <EOL> if self . key and RSA_free : <EOL> RSA_free ( self . key ) </s>
