<s> from __future__ import absolute_import , division , print_function <EOL> import collections <EOL> import os <EOL> import threading <EOL> import types <EOL> import warnings <EOL> from cryptography . exceptions import InternalError <EOL> from cryptography . hazmat . bindings . _openssl import ffi , lib <EOL> from cryptography . hazmat . bindings . openssl . _conditional import CONDITIONAL_NAMES <EOL> _OpenSSLError = collections . namedtuple ( "<STR_LIT>" , <EOL> [ "<STR_LIT:code>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> _OpenSSLErrorWithText = collections . namedtuple ( <EOL> "<STR_LIT>" , [ "<STR_LIT:code>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> ) <EOL> def _consume_errors ( lib ) : <EOL> errors = [ ] <EOL> while True : <EOL> code = lib . ERR_get_error ( ) <EOL> if code == <NUM_LIT:0> : <EOL> break <EOL> err_lib = lib . ERR_GET_LIB ( code ) <EOL> err_func = lib . ERR_GET_FUNC ( code ) <EOL> err_reason = lib . ERR_GET_REASON ( code ) <EOL> errors . append ( _OpenSSLError ( code , err_lib , err_func , err_reason ) ) <EOL> return errors <EOL> def _openssl_assert ( lib , ok ) : <EOL> if not ok : <EOL> errors = _consume_errors ( lib ) <EOL> errors_with_text = [ ] <EOL> for err in errors : <EOL> err_text_reason = ffi . string ( <EOL> lib . ERR_error_string ( err . code , ffi . NULL ) <EOL> ) <EOL> errors_with_text . append ( <EOL> _OpenSSLErrorWithText ( <EOL> err . code , err . lib , err . func , err . reason , err_text_reason <EOL> ) <EOL> ) <EOL> raise InternalError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( errors_with_text ) , <EOL> errors_with_text <EOL> ) <EOL> def ffi_callback ( signature , name , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def wrapper ( func ) : <EOL> if lib . Cryptography_STATIC_CALLBACKS : <EOL> ffi . def_extern ( name = name , ** kwargs ) ( func ) <EOL> callback = getattr ( lib , name ) <EOL> else : <EOL> callback = ffi . callback ( signature , ** kwargs ) ( func ) <EOL> return callback <EOL> return wrapper <EOL> @ ffi_callback ( "<STR_LIT>" , <EOL> name = "<STR_LIT>" , <EOL> error = - <NUM_LIT:1> ) <EOL> def _osrandom_rand_bytes ( buf , size ) : <EOL> signed = ffi . cast ( "<STR_LIT>" , buf ) <EOL> result = os . urandom ( size ) <EOL> signed [ <NUM_LIT:0> : size ] = result <EOL> return <NUM_LIT:1> <EOL> @ ffi_callback ( "<STR_LIT>" , name = "<STR_LIT>" ) <EOL> def _osrandom_rand_status ( ) : <EOL> return <NUM_LIT:1> <EOL> def build_conditional_library ( lib , conditional_names ) : <EOL> conditional_lib = types . ModuleType ( "<STR_LIT>" ) <EOL> excluded_names = set ( ) <EOL> for condition , names in conditional_names . items ( ) : <EOL> if not getattr ( lib , condition ) : <EOL> excluded_names |= set ( names ) <EOL> for attr in dir ( lib ) : <EOL> if attr not in excluded_names : <EOL> setattr ( conditional_lib , attr , getattr ( lib , attr ) ) <EOL> return conditional_lib <EOL> class Binding ( object ) : <EOL> """<STR_LIT>""" <EOL> lib = None <EOL> ffi = ffi <EOL> _lib_loaded = False <EOL> _locks = None <EOL> _lock_cb_handle = None <EOL> _init_lock = threading . Lock ( ) <EOL> _lock_init_lock = threading . Lock ( ) <EOL> _osrandom_engine_id = ffi . new ( "<STR_LIT>" , b"<STR_LIT>" ) <EOL> _osrandom_engine_name = ffi . new ( "<STR_LIT>" , b"<STR_LIT>" ) <EOL> _osrandom_method = ffi . new ( <EOL> "<STR_LIT>" , <EOL> dict ( bytes = _osrandom_rand_bytes , <EOL> pseudorand = _osrandom_rand_bytes , <EOL> status = _osrandom_rand_status ) <EOL> ) <EOL> def __init__ ( self ) : <EOL> self . _ensure_ffi_initialized ( ) <EOL> @ classmethod <EOL> def _register_osrandom_engine ( cls ) : <EOL> _openssl_assert ( cls . lib , cls . lib . ERR_peek_error ( ) == <NUM_LIT:0> ) <EOL> engine = cls . lib . ENGINE_new ( ) <EOL> _openssl_assert ( cls . lib , engine != cls . ffi . NULL ) <EOL> try : <EOL> result = cls . lib . ENGINE_set_id ( engine , cls . _osrandom_engine_id ) <EOL> _openssl_assert ( cls . lib , result == <NUM_LIT:1> ) <EOL> result = cls . lib . ENGINE_set_name ( engine , cls . _osrandom_engine_name ) <EOL> _openssl_assert ( cls . lib , result == <NUM_LIT:1> ) <EOL> result = cls . lib . ENGINE_set_RAND ( engine , cls . _osrandom_method ) <EOL> _openssl_assert ( cls . lib , result == <NUM_LIT:1> ) <EOL> result = cls . lib . ENGINE_add ( engine ) <EOL> if result != <NUM_LIT:1> : <EOL> errors = _consume_errors ( cls . lib ) <EOL> _openssl_assert ( <EOL> cls . lib , <EOL> errors [ <NUM_LIT:0> ] . reason == cls . lib . ENGINE_R_CONFLICTING_ENGINE_ID <EOL> ) <EOL> finally : <EOL> result = cls . lib . ENGINE_free ( engine ) <EOL> _openssl_assert ( cls . lib , result == <NUM_LIT:1> ) <EOL> @ classmethod <EOL> def _ensure_ffi_initialized ( cls ) : <EOL> with cls . _init_lock : <EOL> if not cls . _lib_loaded : <EOL> cls . lib = build_conditional_library ( lib , CONDITIONAL_NAMES ) <EOL> cls . _lib_loaded = True <EOL> cls . lib . SSL_library_init ( ) <EOL> cls . lib . OpenSSL_add_all_algorithms ( ) <EOL> cls . lib . SSL_load_error_strings ( ) <EOL> cls . _register_osrandom_engine ( ) <EOL> @ classmethod <EOL> def init_static_locks ( cls ) : <EOL> with cls . _lock_init_lock : <EOL> cls . _ensure_ffi_initialized ( ) <EOL> if not cls . _lock_cb_handle : <EOL> wrapper = ffi_callback ( <EOL> "<STR_LIT>" , <EOL> name = "<STR_LIT>" , <EOL> ) <EOL> cls . _lock_cb_handle = wrapper ( cls . _lock_cb ) <EOL> __import__ ( "<STR_LIT>" ) <EOL> if cls . lib . CRYPTO_get_locking_callback ( ) != cls . ffi . NULL : <EOL> return <EOL> num_locks = cls . lib . CRYPTO_num_locks ( ) <EOL> cls . _locks = [ threading . Lock ( ) for n in range ( num_locks ) ] <EOL> cls . lib . CRYPTO_set_locking_callback ( cls . _lock_cb_handle ) <EOL> @ classmethod <EOL> def _lock_cb ( cls , mode , n , file , line ) : <EOL> lock = cls . _locks [ n ] <EOL> if mode & cls . lib . CRYPTO_LOCK : <EOL> lock . acquire ( ) <EOL> elif mode & cls . lib . CRYPTO_UNLOCK : <EOL> lock . release ( ) <EOL> else : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" . format ( <EOL> mode , n , file , line <EOL> ) <EOL> ) <EOL> def _verify_openssl_version ( version ) : <EOL> if version < <NUM_LIT> : <EOL> if os . environ . get ( "<STR_LIT>" ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning <EOL> ) <EOL> else : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> elif version < <NUM_LIT> : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning <EOL> ) <EOL> Binding . init_static_locks ( ) <EOL> _verify_openssl_version ( Binding . lib . SSLeay ( ) ) </s>
