<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import glob <EOL> import sys <EOL> import os <EOL> import salt . utils <EOL> from ctypes import cdll , c_char_p , c_int , c_void_p , pointer , create_string_buffer <EOL> from ctypes . util import find_library <EOL> def _load_libcrypto ( ) : <EOL> '''<STR_LIT>''' <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> return cdll . LoadLibrary ( '<STR_LIT>' ) <EOL> elif getattr ( sys , '<STR_LIT>' , False ) and salt . utils . is_smartos ( ) : <EOL> return cdll . LoadLibrary ( glob . glob ( os . path . join ( <EOL> os . path . dirname ( sys . executable ) , <EOL> '<STR_LIT>' ) ) [ <NUM_LIT:0> ] ) <EOL> else : <EOL> lib = find_library ( '<STR_LIT>' ) <EOL> if not lib and salt . utils . is_smartos ( ) : <EOL> lib = glob . glob ( os . path . join ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> lib = lib [ <NUM_LIT:0> ] if len ( lib ) > <NUM_LIT:0> else None <EOL> if lib : <EOL> return cdll . LoadLibrary ( lib ) <EOL> raise OSError ( '<STR_LIT>' ) <EOL> def _init_libcrypto ( ) : <EOL> '''<STR_LIT>''' <EOL> libcrypto = _load_libcrypto ( ) <EOL> libcrypto . RSA_new . argtypes = ( ) <EOL> libcrypto . RSA_new . restype = c_void_p <EOL> libcrypto . RSA_free . argtypes = ( c_void_p , ) <EOL> libcrypto . RSA_size . argtype = ( c_void_p ) <EOL> libcrypto . BIO_new_mem_buf . argtypes = ( c_char_p , c_int ) <EOL> libcrypto . BIO_new_mem_buf . restype = c_void_p <EOL> libcrypto . BIO_free . argtypes = ( c_void_p , ) <EOL> libcrypto . PEM_read_bio_RSAPrivateKey . argtypes = ( c_void_p , c_void_p , c_void_p , c_void_p ) <EOL> libcrypto . PEM_read_bio_RSAPrivateKey . restype = c_void_p <EOL> libcrypto . PEM_read_bio_RSA_PUBKEY . argtypes = ( c_void_p , c_void_p , c_void_p , c_void_p ) <EOL> libcrypto . PEM_read_bio_RSA_PUBKEY . restype = c_void_p <EOL> libcrypto . RSA_private_encrypt . argtypes = ( c_int , c_char_p , c_char_p , c_void_p , c_int ) <EOL> libcrypto . RSA_public_decrypt . argtypes = ( c_int , c_char_p , c_char_p , c_void_p , c_int ) <EOL> libcrypto . OPENSSL_no_config ( ) <EOL> libcrypto . OPENSSL_add_all_algorithms_noconf ( ) <EOL> return libcrypto <EOL> libcrypto = _init_libcrypto ( ) <EOL> RSA_X931_PADDING = <NUM_LIT:5> <EOL> class RSAX931Signer ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , keydata ) : <EOL> '''<STR_LIT>''' <EOL> keydata = salt . utils . to_bytes ( keydata , '<STR_LIT:ascii>' ) <EOL> self . _bio = libcrypto . BIO_new_mem_buf ( keydata , len ( keydata ) ) <EOL> self . _rsa = c_void_p ( libcrypto . RSA_new ( ) ) <EOL> if not libcrypto . PEM_read_bio_RSAPrivateKey ( self . _bio , pointer ( self . _rsa ) , None , None ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def __del__ ( self ) : <EOL> libcrypto . BIO_free ( self . _bio ) <EOL> libcrypto . RSA_free ( self . _rsa ) <EOL> def sign ( self , msg ) : <EOL> '''<STR_LIT>''' <EOL> buf = create_string_buffer ( libcrypto . RSA_size ( self . _rsa ) ) <EOL> msg = salt . utils . to_bytes ( msg ) <EOL> size = libcrypto . RSA_private_encrypt ( len ( msg ) , msg , buf , self . _rsa , RSA_X931_PADDING ) <EOL> if size < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return buf [ <NUM_LIT:0> : size ] <EOL> class RSAX931Verifier ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , pubdata ) : <EOL> '''<STR_LIT>''' <EOL> pubdata = salt . utils . to_bytes ( pubdata , '<STR_LIT:ascii>' ) <EOL> self . _bio = libcrypto . BIO_new_mem_buf ( pubdata , len ( pubdata ) ) <EOL> self . _rsa = c_void_p ( libcrypto . RSA_new ( ) ) <EOL> if not libcrypto . PEM_read_bio_RSA_PUBKEY ( self . _bio , pointer ( self . _rsa ) , None , None ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def __del__ ( self ) : <EOL> libcrypto . BIO_free ( self . _bio ) <EOL> libcrypto . RSA_free ( self . _rsa ) <EOL> def verify ( self , signed ) : <EOL> '''<STR_LIT>''' <EOL> buf = create_string_buffer ( libcrypto . RSA_size ( self . _rsa ) ) <EOL> signed = salt . utils . to_bytes ( signed ) <EOL> size = libcrypto . RSA_public_decrypt ( len ( signed ) , signed , buf , self . _rsa , RSA_X931_PADDING ) <EOL> if size < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return buf [ <NUM_LIT:0> : size ] </s>
