<s> import datetime <EOL> from base64 import b16encode <EOL> from functools import partial <EOL> from operator import __eq__ , __ne__ , __lt__ , __le__ , __gt__ , __ge__ <EOL> from warnings import warn as _warn <EOL> from six import ( <EOL> integer_types as _integer_types , <EOL> text_type as _text_type , <EOL> PY3 as _PY3 ) <EOL> from OpenSSL . _util import ( <EOL> ffi as _ffi , <EOL> lib as _lib , <EOL> exception_from_error_queue as _exception_from_error_queue , <EOL> byte_string as _byte_string , <EOL> native as _native , <EOL> UNSPECIFIED as _UNSPECIFIED , <EOL> text_to_bytes_and_warn as _text_to_bytes_and_warn , <EOL> ) <EOL> FILETYPE_PEM = _lib . SSL_FILETYPE_PEM <EOL> FILETYPE_ASN1 = _lib . SSL_FILETYPE_ASN1 <EOL> FILETYPE_TEXT = <NUM_LIT:2> ** <NUM_LIT:16> - <NUM_LIT:1> <EOL> TYPE_RSA = _lib . EVP_PKEY_RSA <EOL> TYPE_DSA = _lib . EVP_PKEY_DSA <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> _raise_current_error = partial ( _exception_from_error_queue , Error ) <EOL> def _untested_error ( where ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError ( "<STR_LIT>" % ( where , ) ) <EOL> def _new_mem_buf ( buffer = None ) : <EOL> """<STR_LIT>""" <EOL> if buffer is None : <EOL> bio = _lib . BIO_new ( _lib . BIO_s_mem ( ) ) <EOL> free = _lib . BIO_free <EOL> else : <EOL> data = _ffi . new ( "<STR_LIT>" , buffer ) <EOL> bio = _lib . BIO_new_mem_buf ( data , len ( buffer ) ) <EOL> def free ( bio , ref = data ) : <EOL> return _lib . BIO_free ( bio ) <EOL> if bio == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> bio = _ffi . gc ( bio , free ) <EOL> return bio <EOL> def _bio_to_string ( bio ) : <EOL> """<STR_LIT>""" <EOL> result_buffer = _ffi . new ( '<STR_LIT>' ) <EOL> buffer_length = _lib . BIO_get_mem_data ( bio , result_buffer ) <EOL> return _ffi . buffer ( result_buffer [ <NUM_LIT:0> ] , buffer_length ) [ : ] <EOL> def _set_asn1_time ( boundary , when ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( when , bytes ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> set_result = _lib . ASN1_GENERALIZEDTIME_set_string ( <EOL> _ffi . cast ( '<STR_LIT>' , boundary ) , when ) <EOL> if set_result == <NUM_LIT:0> : <EOL> dummy = _ffi . gc ( _lib . ASN1_STRING_new ( ) , _lib . ASN1_STRING_free ) <EOL> _lib . ASN1_STRING_set ( dummy , when , len ( when ) ) <EOL> check_result = _lib . ASN1_GENERALIZEDTIME_check ( <EOL> _ffi . cast ( '<STR_LIT>' , dummy ) ) <EOL> if not check_result : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> _untested_error ( ) <EOL> def _get_asn1_time ( timestamp ) : <EOL> """<STR_LIT>""" <EOL> string_timestamp = _ffi . cast ( '<STR_LIT>' , timestamp ) <EOL> if _lib . ASN1_STRING_length ( string_timestamp ) == <NUM_LIT:0> : <EOL> return None <EOL> elif ( <EOL> _lib . ASN1_STRING_type ( string_timestamp ) == _lib . V_ASN1_GENERALIZEDTIME <EOL> ) : <EOL> return _ffi . string ( _lib . ASN1_STRING_data ( string_timestamp ) ) <EOL> else : <EOL> generalized_timestamp = _ffi . new ( "<STR_LIT>" ) <EOL> _lib . ASN1_TIME_to_generalizedtime ( timestamp , generalized_timestamp ) <EOL> if generalized_timestamp [ <NUM_LIT:0> ] == _ffi . NULL : <EOL> _untested_error ( "<STR_LIT>" ) <EOL> else : <EOL> string_timestamp = _ffi . cast ( <EOL> "<STR_LIT>" , generalized_timestamp [ <NUM_LIT:0> ] ) <EOL> string_data = _lib . ASN1_STRING_data ( string_timestamp ) <EOL> string_result = _ffi . string ( string_data ) <EOL> _lib . ASN1_GENERALIZEDTIME_free ( generalized_timestamp [ <NUM_LIT:0> ] ) <EOL> return string_result <EOL> class PKey ( object ) : <EOL> """<STR_LIT>""" <EOL> _only_public = False <EOL> _initialized = True <EOL> def __init__ ( self ) : <EOL> pkey = _lib . EVP_PKEY_new ( ) <EOL> self . _pkey = _ffi . gc ( pkey , _lib . EVP_PKEY_free ) <EOL> self . _initialized = False <EOL> def generate_key ( self , type , bits ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( type , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not isinstance ( bits , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> exponent = _lib . BN_new ( ) <EOL> exponent = _ffi . gc ( exponent , _lib . BN_free ) <EOL> _lib . BN_set_word ( exponent , _lib . RSA_F4 ) <EOL> if type == TYPE_RSA : <EOL> if bits <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> rsa = _lib . RSA_new ( ) <EOL> result = _lib . RSA_generate_key_ex ( rsa , bits , exponent , _ffi . NULL ) <EOL> if result == <NUM_LIT:0> : <EOL> _raise_current_error ( ) <EOL> result = _lib . EVP_PKEY_assign_RSA ( self . _pkey , rsa ) <EOL> if not result : <EOL> _raise_current_error ( ) <EOL> elif type == TYPE_DSA : <EOL> dsa = _lib . DSA_new ( ) <EOL> if dsa == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> dsa = _ffi . gc ( dsa , _lib . DSA_free ) <EOL> res = _lib . DSA_generate_parameters_ex ( <EOL> dsa , bits , _ffi . NULL , <NUM_LIT:0> , _ffi . NULL , _ffi . NULL , _ffi . NULL <EOL> ) <EOL> if not res == <NUM_LIT:1> : <EOL> _raise_current_error ( ) <EOL> if not _lib . DSA_generate_key ( dsa ) : <EOL> _raise_current_error ( ) <EOL> if not _lib . EVP_PKEY_set1_DSA ( self . _pkey , dsa ) : <EOL> _raise_current_error ( ) <EOL> else : <EOL> raise Error ( "<STR_LIT>" ) <EOL> self . _initialized = True <EOL> def check ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _only_public : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if _lib . EVP_PKEY_type ( self . type ( ) ) != _lib . EVP_PKEY_RSA : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> rsa = _lib . EVP_PKEY_get1_RSA ( self . _pkey ) <EOL> rsa = _ffi . gc ( rsa , _lib . RSA_free ) <EOL> result = _lib . RSA_check_key ( rsa ) <EOL> if result : <EOL> return True <EOL> _raise_current_error ( ) <EOL> def type ( self ) : <EOL> """<STR_LIT>""" <EOL> return _lib . Cryptography_EVP_PKEY_id ( self . _pkey ) <EOL> def bits ( self ) : <EOL> """<STR_LIT>""" <EOL> return _lib . EVP_PKEY_bits ( self . _pkey ) <EOL> PKeyType = PKey <EOL> class _EllipticCurve ( object ) : <EOL> """<STR_LIT>""" <EOL> _curves = None <EOL> if _PY3 : <EOL> def __ne__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , _EllipticCurve ) : <EOL> return super ( _EllipticCurve , self ) . __ne__ ( other ) <EOL> return NotImplemented <EOL> @ classmethod <EOL> def _load_elliptic_curves ( cls , lib ) : <EOL> """<STR_LIT>""" <EOL> if lib . Cryptography_HAS_EC : <EOL> num_curves = lib . EC_get_builtin_curves ( _ffi . NULL , <NUM_LIT:0> ) <EOL> builtin_curves = _ffi . new ( '<STR_LIT>' , num_curves ) <EOL> lib . EC_get_builtin_curves ( builtin_curves , num_curves ) <EOL> return set ( <EOL> cls . from_nid ( lib , c . nid ) <EOL> for c in builtin_curves ) <EOL> return set ( ) <EOL> @ classmethod <EOL> def _get_elliptic_curves ( cls , lib ) : <EOL> """<STR_LIT>""" <EOL> if cls . _curves is None : <EOL> cls . _curves = cls . _load_elliptic_curves ( lib ) <EOL> return cls . _curves <EOL> @ classmethod <EOL> def from_nid ( cls , lib , nid ) : <EOL> """<STR_LIT>""" <EOL> return cls ( lib , nid , _ffi . string ( lib . OBJ_nid2sn ( nid ) ) . decode ( "<STR_LIT:ascii>" ) ) <EOL> def __init__ ( self , lib , nid , name ) : <EOL> """<STR_LIT>""" <EOL> self . _lib = lib <EOL> self . _nid = nid <EOL> self . name = name <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , ) <EOL> def _to_EC_KEY ( self ) : <EOL> """<STR_LIT>""" <EOL> key = self . _lib . EC_KEY_new_by_curve_name ( self . _nid ) <EOL> return _ffi . gc ( key , _lib . EC_KEY_free ) <EOL> def get_elliptic_curves ( ) : <EOL> """<STR_LIT>""" <EOL> return _EllipticCurve . _get_elliptic_curves ( _lib ) <EOL> def get_elliptic_curve ( name ) : <EOL> """<STR_LIT>""" <EOL> for curve in get_elliptic_curves ( ) : <EOL> if curve . name == name : <EOL> return curve <EOL> raise ValueError ( "<STR_LIT>" , name ) <EOL> class X509Name ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> name = _lib . X509_NAME_dup ( name . _name ) <EOL> self . _name = _ffi . gc ( name , _lib . X509_NAME_free ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if name . startswith ( '<STR_LIT:_>' ) : <EOL> return super ( X509Name , self ) . __setattr__ ( name , value ) <EOL> if type ( name ) is not str : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> type ( value ) . __name__ , ) ) <EOL> nid = _lib . OBJ_txt2nid ( _byte_string ( name ) ) <EOL> if nid == _lib . NID_undef : <EOL> try : <EOL> _raise_current_error ( ) <EOL> except Error : <EOL> pass <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> for i in range ( _lib . X509_NAME_entry_count ( self . _name ) ) : <EOL> ent = _lib . X509_NAME_get_entry ( self . _name , i ) <EOL> ent_obj = _lib . X509_NAME_ENTRY_get_object ( ent ) <EOL> ent_nid = _lib . OBJ_obj2nid ( ent_obj ) <EOL> if nid == ent_nid : <EOL> ent = _lib . X509_NAME_delete_entry ( self . _name , i ) <EOL> _lib . X509_NAME_ENTRY_free ( ent ) <EOL> break <EOL> if isinstance ( value , _text_type ) : <EOL> value = value . encode ( '<STR_LIT:utf-8>' ) <EOL> add_result = _lib . X509_NAME_add_entry_by_NID ( <EOL> self . _name , nid , _lib . MBSTRING_UTF8 , value , - <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> if not add_result : <EOL> _raise_current_error ( ) <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> nid = _lib . OBJ_txt2nid ( _byte_string ( name ) ) <EOL> if nid == _lib . NID_undef : <EOL> try : <EOL> _raise_current_error ( ) <EOL> except Error : <EOL> pass <EOL> return super ( X509Name , self ) . __getattr__ ( name ) <EOL> entry_index = _lib . X509_NAME_get_index_by_NID ( self . _name , nid , - <NUM_LIT:1> ) <EOL> if entry_index == - <NUM_LIT:1> : <EOL> return None <EOL> entry = _lib . X509_NAME_get_entry ( self . _name , entry_index ) <EOL> data = _lib . X509_NAME_ENTRY_get_data ( entry ) <EOL> result_buffer = _ffi . new ( "<STR_LIT>" ) <EOL> data_length = _lib . ASN1_STRING_to_UTF8 ( result_buffer , data ) <EOL> if data_length < <NUM_LIT:0> : <EOL> _raise_current_error ( ) <EOL> try : <EOL> result = _ffi . buffer ( <EOL> result_buffer [ <NUM_LIT:0> ] , data_length <EOL> ) [ : ] . decode ( '<STR_LIT:utf-8>' ) <EOL> finally : <EOL> _lib . OPENSSL_free ( result_buffer [ <NUM_LIT:0> ] ) <EOL> return result <EOL> def _cmp ( op ) : <EOL> def f ( self , other ) : <EOL> if not isinstance ( other , X509Name ) : <EOL> return NotImplemented <EOL> result = _lib . X509_NAME_cmp ( self . _name , other . _name ) <EOL> return op ( result , <NUM_LIT:0> ) <EOL> return f <EOL> __eq__ = _cmp ( __eq__ ) <EOL> __ne__ = _cmp ( __ne__ ) <EOL> __lt__ = _cmp ( __lt__ ) <EOL> __le__ = _cmp ( __le__ ) <EOL> __gt__ = _cmp ( __gt__ ) <EOL> __ge__ = _cmp ( __ge__ ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> result_buffer = _ffi . new ( "<STR_LIT>" , <NUM_LIT> ) <EOL> format_result = _lib . X509_NAME_oneline ( <EOL> self . _name , result_buffer , len ( result_buffer ) ) <EOL> if format_result == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> return "<STR_LIT>" % ( <EOL> _native ( _ffi . string ( result_buffer ) ) , ) <EOL> def hash ( self ) : <EOL> """<STR_LIT>""" <EOL> return _lib . X509_NAME_hash ( self . _name ) <EOL> def der ( self ) : <EOL> """<STR_LIT>""" <EOL> result_buffer = _ffi . new ( '<STR_LIT>' ) <EOL> encode_result = _lib . i2d_X509_NAME ( self . _name , result_buffer ) <EOL> if encode_result < <NUM_LIT:0> : <EOL> _raise_current_error ( ) <EOL> string_result = _ffi . buffer ( result_buffer [ <NUM_LIT:0> ] , encode_result ) [ : ] <EOL> _lib . OPENSSL_free ( result_buffer [ <NUM_LIT:0> ] ) <EOL> return string_result <EOL> def get_components ( self ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for i in range ( _lib . X509_NAME_entry_count ( self . _name ) ) : <EOL> ent = _lib . X509_NAME_get_entry ( self . _name , i ) <EOL> fname = _lib . X509_NAME_ENTRY_get_object ( ent ) <EOL> fval = _lib . X509_NAME_ENTRY_get_data ( ent ) <EOL> nid = _lib . OBJ_obj2nid ( fname ) <EOL> name = _lib . OBJ_nid2sn ( nid ) <EOL> result . append ( ( <EOL> _ffi . string ( name ) , <EOL> _ffi . string ( <EOL> _lib . ASN1_STRING_data ( fval ) , <EOL> _lib . ASN1_STRING_length ( fval ) ) ) ) <EOL> return result <EOL> X509NameType = X509Name <EOL> class X509Extension ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , type_name , critical , value , subject = None , issuer = None ) : <EOL> """<STR_LIT>""" <EOL> ctx = _ffi . new ( "<STR_LIT>" ) <EOL> _lib . X509V3_set_ctx ( ctx , _ffi . NULL , _ffi . NULL , _ffi . NULL , _ffi . NULL , <NUM_LIT:0> ) <EOL> _lib . X509V3_set_ctx_nodb ( ctx ) <EOL> if issuer is not None : <EOL> if not isinstance ( issuer , X509 ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> ctx . issuer_cert = issuer . _x509 <EOL> if subject is not None : <EOL> if not isinstance ( subject , X509 ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> ctx . subject_cert = subject . _x509 <EOL> if critical : <EOL> value = b"<STR_LIT>" + value <EOL> extension = _lib . X509V3_EXT_nconf ( _ffi . NULL , ctx , type_name , value ) <EOL> if extension == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> self . _extension = _ffi . gc ( extension , _lib . X509_EXTENSION_free ) <EOL> @ property <EOL> def _nid ( self ) : <EOL> return _lib . OBJ_obj2nid ( <EOL> _lib . X509_EXTENSION_get_object ( self . _extension ) <EOL> ) <EOL> _prefixes = { <EOL> _lib . GEN_EMAIL : "<STR_LIT:email>" , <EOL> _lib . GEN_DNS : "<STR_LIT>" , <EOL> _lib . GEN_URI : "<STR_LIT>" , <EOL> } <EOL> def _subjectAltNameString ( self ) : <EOL> method = _lib . X509V3_EXT_get ( self . _extension ) <EOL> if method == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> ext_data = _lib . X509_EXTENSION_get_data ( self . _extension ) <EOL> payload = ext_data . data <EOL> length = ext_data . length <EOL> payloadptr = _ffi . new ( "<STR_LIT>" ) <EOL> payloadptr [ <NUM_LIT:0> ] = payload <EOL> if method . it != _ffi . NULL : <EOL> ptr = _lib . ASN1_ITEM_ptr ( method . it ) <EOL> data = _lib . ASN1_item_d2i ( _ffi . NULL , payloadptr , length , ptr ) <EOL> names = _ffi . cast ( "<STR_LIT>" , data ) <EOL> else : <EOL> names = _ffi . cast ( <EOL> "<STR_LIT>" , <EOL> method . d2i ( _ffi . NULL , payloadptr , length ) ) <EOL> names = _ffi . gc ( names , _lib . GENERAL_NAMES_free ) <EOL> parts = [ ] <EOL> for i in range ( _lib . sk_GENERAL_NAME_num ( names ) ) : <EOL> name = _lib . sk_GENERAL_NAME_value ( names , i ) <EOL> try : <EOL> label = self . _prefixes [ name . type ] <EOL> except KeyError : <EOL> bio = _new_mem_buf ( ) <EOL> _lib . GENERAL_NAME_print ( bio , name ) <EOL> parts . append ( _native ( _bio_to_string ( bio ) ) ) <EOL> else : <EOL> value = _native ( <EOL> _ffi . buffer ( name . d . ia5 . data , name . d . ia5 . length ) [ : ] ) <EOL> parts . append ( label + "<STR_LIT::>" + value ) <EOL> return "<STR_LIT:U+002CU+0020>" . join ( parts ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if _lib . NID_subject_alt_name == self . _nid : <EOL> return self . _subjectAltNameString ( ) <EOL> bio = _new_mem_buf ( ) <EOL> print_result = _lib . X509V3_EXT_print ( bio , self . _extension , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> if not print_result : <EOL> _raise_current_error ( ) <EOL> return _native ( _bio_to_string ( bio ) ) <EOL> def get_critical ( self ) : <EOL> """<STR_LIT>""" <EOL> return _lib . X509_EXTENSION_get_critical ( self . _extension ) <EOL> def get_short_name ( self ) : <EOL> """<STR_LIT>""" <EOL> obj = _lib . X509_EXTENSION_get_object ( self . _extension ) <EOL> nid = _lib . OBJ_obj2nid ( obj ) <EOL> return _ffi . string ( _lib . OBJ_nid2sn ( nid ) ) <EOL> def get_data ( self ) : <EOL> """<STR_LIT>""" <EOL> octet_result = _lib . X509_EXTENSION_get_data ( self . _extension ) <EOL> string_result = _ffi . cast ( '<STR_LIT>' , octet_result ) <EOL> char_result = _lib . ASN1_STRING_data ( string_result ) <EOL> result_length = _lib . ASN1_STRING_length ( string_result ) <EOL> return _ffi . buffer ( char_result , result_length ) [ : ] <EOL> X509ExtensionType = X509Extension <EOL> class X509Req ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> req = _lib . X509_REQ_new ( ) <EOL> self . _req = _ffi . gc ( req , _lib . X509_REQ_free ) <EOL> def set_pubkey ( self , pkey ) : <EOL> """<STR_LIT>""" <EOL> set_result = _lib . X509_REQ_set_pubkey ( self . _req , pkey . _pkey ) <EOL> if not set_result : <EOL> _raise_current_error ( ) <EOL> def get_pubkey ( self ) : <EOL> """<STR_LIT>""" <EOL> pkey = PKey . __new__ ( PKey ) <EOL> pkey . _pkey = _lib . X509_REQ_get_pubkey ( self . _req ) <EOL> if pkey . _pkey == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> pkey . _pkey = _ffi . gc ( pkey . _pkey , _lib . EVP_PKEY_free ) <EOL> pkey . _only_public = True <EOL> return pkey <EOL> def set_version ( self , version ) : <EOL> """<STR_LIT>""" <EOL> set_result = _lib . X509_REQ_set_version ( self . _req , version ) <EOL> if not set_result : <EOL> _raise_current_error ( ) <EOL> def get_version ( self ) : <EOL> """<STR_LIT>""" <EOL> return _lib . X509_REQ_get_version ( self . _req ) <EOL> def get_subject ( self ) : <EOL> """<STR_LIT>""" <EOL> name = X509Name . __new__ ( X509Name ) <EOL> name . _name = _lib . X509_REQ_get_subject_name ( self . _req ) <EOL> if name . _name == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> name . _owner = self <EOL> return name <EOL> def add_extensions ( self , extensions ) : <EOL> """<STR_LIT>""" <EOL> stack = _lib . sk_X509_EXTENSION_new_null ( ) <EOL> if stack == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> stack = _ffi . gc ( stack , _lib . sk_X509_EXTENSION_free ) <EOL> for ext in extensions : <EOL> if not isinstance ( ext , X509Extension ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> _lib . sk_X509_EXTENSION_push ( stack , ext . _extension ) <EOL> add_result = _lib . X509_REQ_add_extensions ( self . _req , stack ) <EOL> if not add_result : <EOL> _raise_current_error ( ) <EOL> def get_extensions ( self ) : <EOL> """<STR_LIT>""" <EOL> exts = [ ] <EOL> native_exts_obj = _lib . X509_REQ_get_extensions ( self . _req ) <EOL> for i in range ( _lib . sk_X509_EXTENSION_num ( native_exts_obj ) ) : <EOL> ext = X509Extension . __new__ ( X509Extension ) <EOL> ext . _extension = _lib . sk_X509_EXTENSION_value ( native_exts_obj , i ) <EOL> exts . append ( ext ) <EOL> return exts <EOL> def sign ( self , pkey , digest ) : <EOL> """<STR_LIT>""" <EOL> if pkey . _only_public : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not pkey . _initialized : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> digest_obj = _lib . EVP_get_digestbyname ( _byte_string ( digest ) ) <EOL> if digest_obj == _ffi . NULL : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> sign_result = _lib . X509_REQ_sign ( self . _req , pkey . _pkey , digest_obj ) <EOL> if not sign_result : <EOL> _raise_current_error ( ) <EOL> def verify ( self , pkey ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( pkey , PKey ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> result = _lib . X509_REQ_verify ( self . _req , pkey . _pkey ) <EOL> if result <= <NUM_LIT:0> : <EOL> _raise_current_error ( ) <EOL> return result <EOL> X509ReqType = X509Req <EOL> class X509 ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> x509 = _lib . X509_new ( ) <EOL> self . _x509 = _ffi . gc ( x509 , _lib . X509_free ) <EOL> def set_version ( self , version ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( version , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> _lib . X509_set_version ( self . _x509 , version ) <EOL> def get_version ( self ) : <EOL> """<STR_LIT>""" <EOL> return _lib . X509_get_version ( self . _x509 ) <EOL> def get_pubkey ( self ) : <EOL> """<STR_LIT>""" <EOL> pkey = PKey . __new__ ( PKey ) <EOL> pkey . _pkey = _lib . X509_get_pubkey ( self . _x509 ) <EOL> if pkey . _pkey == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> pkey . _pkey = _ffi . gc ( pkey . _pkey , _lib . EVP_PKEY_free ) <EOL> pkey . _only_public = True <EOL> return pkey <EOL> def set_pubkey ( self , pkey ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( pkey , PKey ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> set_result = _lib . X509_set_pubkey ( self . _x509 , pkey . _pkey ) <EOL> if not set_result : <EOL> _raise_current_error ( ) <EOL> def sign ( self , pkey , digest ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( pkey , PKey ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if pkey . _only_public : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not pkey . _initialized : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> evp_md = _lib . EVP_get_digestbyname ( _byte_string ( digest ) ) <EOL> if evp_md == _ffi . NULL : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> sign_result = _lib . X509_sign ( self . _x509 , pkey . _pkey , evp_md ) <EOL> if not sign_result : <EOL> _raise_current_error ( ) <EOL> def get_signature_algorithm ( self ) : <EOL> """<STR_LIT>""" <EOL> alg = self . _x509 . cert_info . signature . algorithm <EOL> nid = _lib . OBJ_obj2nid ( alg ) <EOL> if nid == _lib . NID_undef : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return _ffi . string ( _lib . OBJ_nid2ln ( nid ) ) <EOL> def digest ( self , digest_name ) : <EOL> """<STR_LIT>""" <EOL> digest = _lib . EVP_get_digestbyname ( _byte_string ( digest_name ) ) <EOL> if digest == _ffi . NULL : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> result_buffer = _ffi . new ( "<STR_LIT>" , _lib . EVP_MAX_MD_SIZE ) <EOL> result_length = _ffi . new ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> result_length [ <NUM_LIT:0> ] = len ( result_buffer ) <EOL> digest_result = _lib . X509_digest ( <EOL> self . _x509 , digest , result_buffer , result_length ) <EOL> if not digest_result : <EOL> _raise_current_error ( ) <EOL> return b"<STR_LIT::>" . join ( [ <EOL> b16encode ( ch ) . upper ( ) for ch <EOL> in _ffi . buffer ( result_buffer , result_length [ <NUM_LIT:0> ] ) ] ) <EOL> def subject_name_hash ( self ) : <EOL> """<STR_LIT>""" <EOL> return _lib . X509_subject_name_hash ( self . _x509 ) <EOL> def set_serial_number ( self , serial ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( serial , _integer_types ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> hex_serial = hex ( serial ) [ <NUM_LIT:2> : ] <EOL> if not isinstance ( hex_serial , bytes ) : <EOL> hex_serial = hex_serial . encode ( '<STR_LIT:ascii>' ) <EOL> bignum_serial = _ffi . new ( "<STR_LIT>" ) <EOL> small_serial = _lib . BN_hex2bn ( bignum_serial , hex_serial ) <EOL> if bignum_serial [ <NUM_LIT:0> ] == _ffi . NULL : <EOL> set_result = _lib . ASN1_INTEGER_set ( <EOL> _lib . X509_get_serialNumber ( self . _x509 ) , small_serial ) <EOL> if set_result : <EOL> _raise_current_error ( ) <EOL> else : <EOL> asn1_serial = _lib . BN_to_ASN1_INTEGER ( bignum_serial [ <NUM_LIT:0> ] , _ffi . NULL ) <EOL> _lib . BN_free ( bignum_serial [ <NUM_LIT:0> ] ) <EOL> if asn1_serial == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> asn1_serial = _ffi . gc ( asn1_serial , _lib . ASN1_INTEGER_free ) <EOL> set_result = _lib . X509_set_serialNumber ( self . _x509 , asn1_serial ) <EOL> if not set_result : <EOL> _raise_current_error ( ) <EOL> def get_serial_number ( self ) : <EOL> """<STR_LIT>""" <EOL> asn1_serial = _lib . X509_get_serialNumber ( self . _x509 ) <EOL> bignum_serial = _lib . ASN1_INTEGER_to_BN ( asn1_serial , _ffi . NULL ) <EOL> try : <EOL> hex_serial = _lib . BN_bn2hex ( bignum_serial ) <EOL> try : <EOL> hexstring_serial = _ffi . string ( hex_serial ) <EOL> serial = int ( hexstring_serial , <NUM_LIT:16> ) <EOL> return serial <EOL> finally : <EOL> _lib . OPENSSL_free ( hex_serial ) <EOL> finally : <EOL> _lib . BN_free ( bignum_serial ) <EOL> def gmtime_adj_notAfter ( self , amount ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( amount , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> notAfter = _lib . X509_get_notAfter ( self . _x509 ) <EOL> _lib . X509_gmtime_adj ( notAfter , amount ) <EOL> def gmtime_adj_notBefore ( self , amount ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( amount , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> notBefore = _lib . X509_get_notBefore ( self . _x509 ) <EOL> _lib . X509_gmtime_adj ( notBefore , amount ) <EOL> def has_expired ( self ) : <EOL> """<STR_LIT>""" <EOL> time_string = _native ( self . get_notAfter ( ) ) <EOL> not_after = datetime . datetime . strptime ( time_string , "<STR_LIT>" ) <EOL> return not_after < datetime . datetime . utcnow ( ) <EOL> def _get_boundary_time ( self , which ) : <EOL> return _get_asn1_time ( which ( self . _x509 ) ) <EOL> def get_notBefore ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_boundary_time ( _lib . X509_get_notBefore ) <EOL> def _set_boundary_time ( self , which , when ) : <EOL> return _set_asn1_time ( which ( self . _x509 ) , when ) <EOL> def set_notBefore ( self , when ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_boundary_time ( _lib . X509_get_notBefore , when ) <EOL> def get_notAfter ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_boundary_time ( _lib . X509_get_notAfter ) <EOL> def set_notAfter ( self , when ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_boundary_time ( _lib . X509_get_notAfter , when ) <EOL> def _get_name ( self , which ) : <EOL> name = X509Name . __new__ ( X509Name ) <EOL> name . _name = which ( self . _x509 ) <EOL> if name . _name == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> name . _owner = self <EOL> return name <EOL> def _set_name ( self , which , name ) : <EOL> if not isinstance ( name , X509Name ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> set_result = which ( self . _x509 , name . _name ) <EOL> if not set_result : <EOL> _raise_current_error ( ) <EOL> def get_issuer ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_name ( _lib . X509_get_issuer_name ) <EOL> def set_issuer ( self , issuer ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_name ( _lib . X509_set_issuer_name , issuer ) <EOL> def get_subject ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_name ( _lib . X509_get_subject_name ) <EOL> def set_subject ( self , subject ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_name ( _lib . X509_set_subject_name , subject ) <EOL> def get_extension_count ( self ) : <EOL> """<STR_LIT>""" <EOL> return _lib . X509_get_ext_count ( self . _x509 ) <EOL> def add_extensions ( self , extensions ) : <EOL> """<STR_LIT>""" <EOL> for ext in extensions : <EOL> if not isinstance ( ext , X509Extension ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> add_result = _lib . X509_add_ext ( self . _x509 , ext . _extension , - <NUM_LIT:1> ) <EOL> if not add_result : <EOL> _raise_current_error ( ) <EOL> def get_extension ( self , index ) : <EOL> """<STR_LIT>""" <EOL> ext = X509Extension . __new__ ( X509Extension ) <EOL> ext . _extension = _lib . X509_get_ext ( self . _x509 , index ) <EOL> if ext . _extension == _ffi . NULL : <EOL> raise IndexError ( "<STR_LIT>" ) <EOL> extension = _lib . X509_EXTENSION_dup ( ext . _extension ) <EOL> ext . _extension = _ffi . gc ( extension , _lib . X509_EXTENSION_free ) <EOL> return ext <EOL> X509Type = X509 <EOL> class X509Store ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> store = _lib . X509_STORE_new ( ) <EOL> self . _store = _ffi . gc ( store , _lib . X509_STORE_free ) <EOL> def add_cert ( self , cert ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( cert , X509 ) : <EOL> raise TypeError ( ) <EOL> result = _lib . X509_STORE_add_cert ( self . _store , cert . _x509 ) <EOL> if not result : <EOL> _raise_current_error ( ) <EOL> X509StoreType = X509Store <EOL> class X509StoreContextError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message , certificate ) : <EOL> super ( X509StoreContextError , self ) . __init__ ( message ) <EOL> self . certificate = certificate <EOL> class X509StoreContext ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , store , certificate ) : <EOL> store_ctx = _lib . X509_STORE_CTX_new ( ) <EOL> self . _store_ctx = _ffi . gc ( store_ctx , _lib . X509_STORE_CTX_free ) <EOL> self . _store = store <EOL> self . _cert = certificate <EOL> self . _init ( ) <EOL> def _init ( self ) : <EOL> """<STR_LIT>""" <EOL> ret = _lib . X509_STORE_CTX_init ( <EOL> self . _store_ctx , self . _store . _store , self . _cert . _x509 , _ffi . NULL <EOL> ) <EOL> if ret <= <NUM_LIT:0> : <EOL> _raise_current_error ( ) <EOL> def _cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> _lib . X509_STORE_CTX_cleanup ( self . _store_ctx ) <EOL> def _exception_from_context ( self ) : <EOL> """<STR_LIT>""" <EOL> errors = [ <EOL> _lib . X509_STORE_CTX_get_error ( self . _store_ctx ) , <EOL> _lib . X509_STORE_CTX_get_error_depth ( self . _store_ctx ) , <EOL> _native ( _ffi . string ( _lib . X509_verify_cert_error_string ( <EOL> _lib . X509_STORE_CTX_get_error ( self . _store_ctx ) ) ) ) , <EOL> ] <EOL> _x509 = _lib . X509_STORE_CTX_get_current_cert ( self . _store_ctx ) <EOL> _cert = _lib . X509_dup ( _x509 ) <EOL> pycert = X509 . __new__ ( X509 ) <EOL> pycert . _x509 = _ffi . gc ( _cert , _lib . X509_free ) <EOL> return X509StoreContextError ( errors , pycert ) <EOL> def set_store ( self , store ) : <EOL> """<STR_LIT>""" <EOL> self . _store = store <EOL> def verify_certificate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _init ( ) <EOL> ret = _lib . X509_verify_cert ( self . _store_ctx ) <EOL> self . _cleanup ( ) <EOL> if ret <= <NUM_LIT:0> : <EOL> raise self . _exception_from_context ( ) <EOL> def load_certificate ( type , buffer ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( buffer , _text_type ) : <EOL> buffer = buffer . encode ( "<STR_LIT:ascii>" ) <EOL> bio = _new_mem_buf ( buffer ) <EOL> if type == FILETYPE_PEM : <EOL> x509 = _lib . PEM_read_bio_X509 ( bio , _ffi . NULL , _ffi . NULL , _ffi . NULL ) <EOL> elif type == FILETYPE_ASN1 : <EOL> x509 = _lib . d2i_X509_bio ( bio , _ffi . NULL ) <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> if x509 == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> cert = X509 . __new__ ( X509 ) <EOL> cert . _x509 = _ffi . gc ( x509 , _lib . X509_free ) <EOL> return cert <EOL> def dump_certificate ( type , cert ) : <EOL> """<STR_LIT>""" <EOL> bio = _new_mem_buf ( ) <EOL> if type == FILETYPE_PEM : <EOL> result_code = _lib . PEM_write_bio_X509 ( bio , cert . _x509 ) <EOL> elif type == FILETYPE_ASN1 : <EOL> result_code = _lib . i2d_X509_bio ( bio , cert . _x509 ) <EOL> elif type == FILETYPE_TEXT : <EOL> result_code = _lib . X509_print_ex ( bio , cert . _x509 , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> assert result_code == <NUM_LIT:1> <EOL> return _bio_to_string ( bio ) <EOL> def dump_publickey ( type , pkey ) : <EOL> """<STR_LIT>""" <EOL> bio = _new_mem_buf ( ) <EOL> if type == FILETYPE_PEM : <EOL> write_bio = _lib . PEM_write_bio_PUBKEY <EOL> elif type == FILETYPE_ASN1 : <EOL> write_bio = _lib . i2d_PUBKEY_bio <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> result_code = write_bio ( bio , pkey . _pkey ) <EOL> if result_code != <NUM_LIT:1> : <EOL> _raise_current_error ( ) <EOL> return _bio_to_string ( bio ) <EOL> def dump_privatekey ( type , pkey , cipher = None , passphrase = None ) : <EOL> """<STR_LIT>""" <EOL> bio = _new_mem_buf ( ) <EOL> if cipher is not None : <EOL> if passphrase is None : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> cipher_obj = _lib . EVP_get_cipherbyname ( _byte_string ( cipher ) ) <EOL> if cipher_obj == _ffi . NULL : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> cipher_obj = _ffi . NULL <EOL> helper = _PassphraseHelper ( type , passphrase ) <EOL> if type == FILETYPE_PEM : <EOL> result_code = _lib . PEM_write_bio_PrivateKey ( <EOL> bio , pkey . _pkey , cipher_obj , _ffi . NULL , <NUM_LIT:0> , <EOL> helper . callback , helper . callback_args ) <EOL> helper . raise_if_problem ( ) <EOL> elif type == FILETYPE_ASN1 : <EOL> result_code = _lib . i2d_PrivateKey_bio ( bio , pkey . _pkey ) <EOL> elif type == FILETYPE_TEXT : <EOL> rsa = _lib . EVP_PKEY_get1_RSA ( pkey . _pkey ) <EOL> result_code = _lib . RSA_print ( bio , rsa , <NUM_LIT:0> ) <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if result_code == <NUM_LIT:0> : <EOL> _raise_current_error ( ) <EOL> return _bio_to_string ( bio ) <EOL> class Revoked ( object ) : <EOL> """<STR_LIT>""" <EOL> _crl_reasons = [ <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> b"<STR_LIT>" , <EOL> ] <EOL> def __init__ ( self ) : <EOL> revoked = _lib . X509_REVOKED_new ( ) <EOL> self . _revoked = _ffi . gc ( revoked , _lib . X509_REVOKED_free ) <EOL> def set_serial ( self , hex_str ) : <EOL> """<STR_LIT>""" <EOL> bignum_serial = _ffi . gc ( _lib . BN_new ( ) , _lib . BN_free ) <EOL> bignum_ptr = _ffi . new ( "<STR_LIT>" ) <EOL> bignum_ptr [ <NUM_LIT:0> ] = bignum_serial <EOL> bn_result = _lib . BN_hex2bn ( bignum_ptr , hex_str ) <EOL> if not bn_result : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> asn1_serial = _ffi . gc ( <EOL> _lib . BN_to_ASN1_INTEGER ( bignum_serial , _ffi . NULL ) , <EOL> _lib . ASN1_INTEGER_free ) <EOL> _lib . X509_REVOKED_set_serialNumber ( self . _revoked , asn1_serial ) <EOL> def get_serial ( self ) : <EOL> """<STR_LIT>""" <EOL> bio = _new_mem_buf ( ) <EOL> result = _lib . i2a_ASN1_INTEGER ( bio , self . _revoked . serialNumber ) <EOL> if result < <NUM_LIT:0> : <EOL> _raise_current_error ( ) <EOL> return _bio_to_string ( bio ) <EOL> def _delete_reason ( self ) : <EOL> stack = self . _revoked . extensions <EOL> for i in range ( _lib . sk_X509_EXTENSION_num ( stack ) ) : <EOL> ext = _lib . sk_X509_EXTENSION_value ( stack , i ) <EOL> obj = _lib . X509_EXTENSION_get_object ( ext ) <EOL> if _lib . OBJ_obj2nid ( obj ) == _lib . NID_crl_reason : <EOL> _lib . X509_EXTENSION_free ( ext ) <EOL> _lib . sk_X509_EXTENSION_delete ( stack , i ) <EOL> break <EOL> def set_reason ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> if reason is None : <EOL> self . _delete_reason ( ) <EOL> elif not isinstance ( reason , bytes ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> else : <EOL> reason = reason . lower ( ) . replace ( b'<STR_LIT:U+0020>' , b'<STR_LIT>' ) <EOL> reason_code = [ r . lower ( ) for r in self . _crl_reasons ] . index ( reason ) <EOL> new_reason_ext = _lib . ASN1_ENUMERATED_new ( ) <EOL> if new_reason_ext == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> new_reason_ext = _ffi . gc ( new_reason_ext , _lib . ASN1_ENUMERATED_free ) <EOL> set_result = _lib . ASN1_ENUMERATED_set ( new_reason_ext , reason_code ) <EOL> if set_result == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> self . _delete_reason ( ) <EOL> add_result = _lib . X509_REVOKED_add1_ext_i2d ( <EOL> self . _revoked , _lib . NID_crl_reason , new_reason_ext , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> if not add_result : <EOL> _raise_current_error ( ) <EOL> def get_reason ( self ) : <EOL> """<STR_LIT>""" <EOL> extensions = self . _revoked . extensions <EOL> for i in range ( _lib . sk_X509_EXTENSION_num ( extensions ) ) : <EOL> ext = _lib . sk_X509_EXTENSION_value ( extensions , i ) <EOL> obj = _lib . X509_EXTENSION_get_object ( ext ) <EOL> if _lib . OBJ_obj2nid ( obj ) == _lib . NID_crl_reason : <EOL> bio = _new_mem_buf ( ) <EOL> print_result = _lib . X509V3_EXT_print ( bio , ext , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> if not print_result : <EOL> print_result = _lib . M_ASN1_OCTET_STRING_print ( <EOL> bio , _lib . X509_EXTENSION_get_data ( ext ) <EOL> ) <EOL> if print_result == <NUM_LIT:0> : <EOL> _raise_current_error ( ) <EOL> return _bio_to_string ( bio ) <EOL> def all_reasons ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _crl_reasons [ : ] <EOL> def set_rev_date ( self , when ) : <EOL> """<STR_LIT>""" <EOL> return _set_asn1_time ( self . _revoked . revocationDate , when ) <EOL> def get_rev_date ( self ) : <EOL> """<STR_LIT>""" <EOL> return _get_asn1_time ( self . _revoked . revocationDate ) <EOL> class CRL ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> crl = _lib . X509_CRL_new ( ) <EOL> self . _crl = _ffi . gc ( crl , _lib . X509_CRL_free ) <EOL> def get_revoked ( self ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> revoked_stack = self . _crl . crl . revoked <EOL> for i in range ( _lib . sk_X509_REVOKED_num ( revoked_stack ) ) : <EOL> revoked = _lib . sk_X509_REVOKED_value ( revoked_stack , i ) <EOL> revoked_copy = _lib . Cryptography_X509_REVOKED_dup ( revoked ) <EOL> pyrev = Revoked . __new__ ( Revoked ) <EOL> pyrev . _revoked = _ffi . gc ( revoked_copy , _lib . X509_REVOKED_free ) <EOL> results . append ( pyrev ) <EOL> if results : <EOL> return tuple ( results ) <EOL> def add_revoked ( self , revoked ) : <EOL> """<STR_LIT>""" <EOL> copy = _lib . Cryptography_X509_REVOKED_dup ( revoked . _revoked ) <EOL> if copy == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> add_result = _lib . X509_CRL_add0_revoked ( self . _crl , copy ) <EOL> if add_result == <NUM_LIT:0> : <EOL> _raise_current_error ( ) <EOL> def export ( self , cert , key , type = FILETYPE_PEM , days = <NUM_LIT:100> , <EOL> digest = _UNSPECIFIED ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( cert , X509 ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not isinstance ( key , PKey ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not isinstance ( type , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if digest is _UNSPECIFIED : <EOL> _warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> category = DeprecationWarning , <EOL> stacklevel = <NUM_LIT:2> , <EOL> ) <EOL> digest = b"<STR_LIT>" <EOL> digest_obj = _lib . EVP_get_digestbyname ( digest ) <EOL> if digest_obj == _ffi . NULL : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> bio = _lib . BIO_new ( _lib . BIO_s_mem ( ) ) <EOL> if bio == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> sometime = _lib . ASN1_TIME_new ( ) <EOL> if sometime == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> _lib . X509_gmtime_adj ( sometime , <NUM_LIT:0> ) <EOL> _lib . X509_CRL_set_lastUpdate ( self . _crl , sometime ) <EOL> _lib . X509_gmtime_adj ( sometime , days * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) <EOL> _lib . X509_CRL_set_nextUpdate ( self . _crl , sometime ) <EOL> _lib . X509_CRL_set_issuer_name ( <EOL> self . _crl , _lib . X509_get_subject_name ( cert . _x509 ) <EOL> ) <EOL> sign_result = _lib . X509_CRL_sign ( self . _crl , key . _pkey , digest_obj ) <EOL> if not sign_result : <EOL> _raise_current_error ( ) <EOL> return dump_crl ( type , self ) <EOL> CRLType = CRL <EOL> class PKCS7 ( object ) : <EOL> def type_is_signed ( self ) : <EOL> """<STR_LIT>""" <EOL> if _lib . PKCS7_type_is_signed ( self . _pkcs7 ) : <EOL> return True <EOL> return False <EOL> def type_is_enveloped ( self ) : <EOL> """<STR_LIT>""" <EOL> if _lib . PKCS7_type_is_enveloped ( self . _pkcs7 ) : <EOL> return True <EOL> return False <EOL> def type_is_signedAndEnveloped ( self ) : <EOL> """<STR_LIT>""" <EOL> if _lib . PKCS7_type_is_signedAndEnveloped ( self . _pkcs7 ) : <EOL> return True <EOL> return False <EOL> def type_is_data ( self ) : <EOL> """<STR_LIT>""" <EOL> if _lib . PKCS7_type_is_data ( self . _pkcs7 ) : <EOL> return True <EOL> return False <EOL> def get_type_name ( self ) : <EOL> """<STR_LIT>""" <EOL> nid = _lib . OBJ_obj2nid ( self . _pkcs7 . type ) <EOL> string_type = _lib . OBJ_nid2sn ( nid ) <EOL> return _ffi . string ( string_type ) <EOL> PKCS7Type = PKCS7 <EOL> class PKCS12 ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _pkey = None <EOL> self . _cert = None <EOL> self . _cacerts = None <EOL> self . _friendlyname = None <EOL> def get_certificate ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _cert <EOL> def set_certificate ( self , cert ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( cert , X509 ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . _cert = cert <EOL> def get_privatekey ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _pkey <EOL> def set_privatekey ( self , pkey ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( pkey , PKey ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . _pkey = pkey <EOL> def get_ca_certificates ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _cacerts is not None : <EOL> return tuple ( self . _cacerts ) <EOL> def set_ca_certificates ( self , cacerts ) : <EOL> """<STR_LIT>""" <EOL> if cacerts is None : <EOL> self . _cacerts = None <EOL> else : <EOL> cacerts = list ( cacerts ) <EOL> for cert in cacerts : <EOL> if not isinstance ( cert , X509 ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> self . _cacerts = cacerts <EOL> def set_friendlyname ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name is None : <EOL> self . _friendlyname = None <EOL> elif not isinstance ( name , bytes ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % ( name , ) <EOL> ) <EOL> self . _friendlyname = name <EOL> def get_friendlyname ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _friendlyname <EOL> def export ( self , passphrase = None , iter = <NUM_LIT> , maciter = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> passphrase = _text_to_bytes_and_warn ( "<STR_LIT>" , passphrase ) <EOL> if self . _cacerts is None : <EOL> cacerts = _ffi . NULL <EOL> else : <EOL> cacerts = _lib . sk_X509_new_null ( ) <EOL> cacerts = _ffi . gc ( cacerts , _lib . sk_X509_free ) <EOL> for cert in self . _cacerts : <EOL> _lib . sk_X509_push ( cacerts , cert . _x509 ) <EOL> if passphrase is None : <EOL> passphrase = _ffi . NULL <EOL> friendlyname = self . _friendlyname <EOL> if friendlyname is None : <EOL> friendlyname = _ffi . NULL <EOL> if self . _pkey is None : <EOL> pkey = _ffi . NULL <EOL> else : <EOL> pkey = self . _pkey . _pkey <EOL> if self . _cert is None : <EOL> cert = _ffi . NULL <EOL> else : <EOL> cert = self . _cert . _x509 <EOL> pkcs12 = _lib . PKCS12_create ( <EOL> passphrase , friendlyname , pkey , cert , cacerts , <EOL> _lib . NID_pbe_WithSHA1And3_Key_TripleDES_CBC , <EOL> _lib . NID_pbe_WithSHA1And3_Key_TripleDES_CBC , <EOL> iter , maciter , <NUM_LIT:0> ) <EOL> if pkcs12 == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> pkcs12 = _ffi . gc ( pkcs12 , _lib . PKCS12_free ) <EOL> bio = _new_mem_buf ( ) <EOL> _lib . i2d_PKCS12_bio ( bio , pkcs12 ) <EOL> return _bio_to_string ( bio ) <EOL> PKCS12Type = PKCS12 <EOL> class NetscapeSPKI ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> spki = _lib . NETSCAPE_SPKI_new ( ) <EOL> self . _spki = _ffi . gc ( spki , _lib . NETSCAPE_SPKI_free ) <EOL> def sign ( self , pkey , digest ) : <EOL> """<STR_LIT>""" <EOL> if pkey . _only_public : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not pkey . _initialized : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> digest_obj = _lib . EVP_get_digestbyname ( _byte_string ( digest ) ) <EOL> if digest_obj == _ffi . NULL : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> sign_result = _lib . NETSCAPE_SPKI_sign ( <EOL> self . _spki , pkey . _pkey , digest_obj <EOL> ) <EOL> if not sign_result : <EOL> _raise_current_error ( ) <EOL> def verify ( self , key ) : <EOL> """<STR_LIT>""" <EOL> answer = _lib . NETSCAPE_SPKI_verify ( self . _spki , key . _pkey ) <EOL> if answer <= <NUM_LIT:0> : <EOL> _raise_current_error ( ) <EOL> return True <EOL> def b64_encode ( self ) : <EOL> """<STR_LIT>""" <EOL> encoded = _lib . NETSCAPE_SPKI_b64_encode ( self . _spki ) <EOL> result = _ffi . string ( encoded ) <EOL> _lib . OPENSSL_free ( encoded ) <EOL> return result <EOL> def get_pubkey ( self ) : <EOL> """<STR_LIT>""" <EOL> pkey = PKey . __new__ ( PKey ) <EOL> pkey . _pkey = _lib . NETSCAPE_SPKI_get_pubkey ( self . _spki ) <EOL> if pkey . _pkey == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> pkey . _pkey = _ffi . gc ( pkey . _pkey , _lib . EVP_PKEY_free ) <EOL> pkey . _only_public = True <EOL> return pkey <EOL> def set_pubkey ( self , pkey ) : <EOL> """<STR_LIT>""" <EOL> set_result = _lib . NETSCAPE_SPKI_set_pubkey ( self . _spki , pkey . _pkey ) <EOL> if not set_result : <EOL> _raise_current_error ( ) <EOL> NetscapeSPKIType = NetscapeSPKI <EOL> class _PassphraseHelper ( object ) : <EOL> def __init__ ( self , type , passphrase , more_args = False , truncate = False ) : <EOL> if type != FILETYPE_PEM and passphrase is not None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> self . _passphrase = passphrase <EOL> self . _more_args = more_args <EOL> self . _truncate = truncate <EOL> self . _problems = [ ] <EOL> @ property <EOL> def callback ( self ) : <EOL> if self . _passphrase is None : <EOL> return _ffi . NULL <EOL> elif isinstance ( self . _passphrase , bytes ) : <EOL> return _ffi . NULL <EOL> elif callable ( self . _passphrase ) : <EOL> return _ffi . callback ( "<STR_LIT>" , self . _read_passphrase ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> @ property <EOL> def callback_args ( self ) : <EOL> if self . _passphrase is None : <EOL> return _ffi . NULL <EOL> elif isinstance ( self . _passphrase , bytes ) : <EOL> return self . _passphrase <EOL> elif callable ( self . _passphrase ) : <EOL> return _ffi . NULL <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def raise_if_problem ( self , exceptionType = Error ) : <EOL> try : <EOL> _exception_from_error_queue ( exceptionType ) <EOL> except exceptionType as e : <EOL> from_queue = e <EOL> if self . _problems : <EOL> raise self . _problems [ <NUM_LIT:0> ] <EOL> return from_queue <EOL> def _read_passphrase ( self , buf , size , rwflag , userdata ) : <EOL> try : <EOL> if self . _more_args : <EOL> result = self . _passphrase ( size , rwflag , userdata ) <EOL> else : <EOL> result = self . _passphrase ( rwflag ) <EOL> if not isinstance ( result , bytes ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if len ( result ) > size : <EOL> if self . _truncate : <EOL> result = result [ : size ] <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> for i in range ( len ( result ) ) : <EOL> buf [ i ] = result [ i : i + <NUM_LIT:1> ] <EOL> return len ( result ) <EOL> except Exception as e : <EOL> self . _problems . append ( e ) <EOL> return <NUM_LIT:0> <EOL> def load_publickey ( type , buffer ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( buffer , _text_type ) : <EOL> buffer = buffer . encode ( "<STR_LIT:ascii>" ) <EOL> bio = _new_mem_buf ( buffer ) <EOL> if type == FILETYPE_PEM : <EOL> evp_pkey = _lib . PEM_read_bio_PUBKEY ( <EOL> bio , _ffi . NULL , _ffi . NULL , _ffi . NULL ) <EOL> elif type == FILETYPE_ASN1 : <EOL> evp_pkey = _lib . d2i_PUBKEY_bio ( bio , _ffi . NULL ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if evp_pkey == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> pkey = PKey . __new__ ( PKey ) <EOL> pkey . _pkey = _ffi . gc ( evp_pkey , _lib . EVP_PKEY_free ) <EOL> return pkey <EOL> def load_privatekey ( type , buffer , passphrase = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( buffer , _text_type ) : <EOL> buffer = buffer . encode ( "<STR_LIT:ascii>" ) <EOL> bio = _new_mem_buf ( buffer ) <EOL> helper = _PassphraseHelper ( type , passphrase ) <EOL> if type == FILETYPE_PEM : <EOL> evp_pkey = _lib . PEM_read_bio_PrivateKey ( <EOL> bio , _ffi . NULL , helper . callback , helper . callback_args ) <EOL> helper . raise_if_problem ( ) <EOL> elif type == FILETYPE_ASN1 : <EOL> evp_pkey = _lib . d2i_PrivateKey_bio ( bio , _ffi . NULL ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if evp_pkey == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> pkey = PKey . __new__ ( PKey ) <EOL> pkey . _pkey = _ffi . gc ( evp_pkey , _lib . EVP_PKEY_free ) <EOL> return pkey <EOL> def dump_certificate_request ( type , req ) : <EOL> """<STR_LIT>""" <EOL> bio = _new_mem_buf ( ) <EOL> if type == FILETYPE_PEM : <EOL> result_code = _lib . PEM_write_bio_X509_REQ ( bio , req . _req ) <EOL> elif type == FILETYPE_ASN1 : <EOL> result_code = _lib . i2d_X509_REQ_bio ( bio , req . _req ) <EOL> elif type == FILETYPE_TEXT : <EOL> result_code = _lib . X509_REQ_print_ex ( bio , req . _req , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> if result_code == <NUM_LIT:0> : <EOL> _raise_current_error ( ) <EOL> return _bio_to_string ( bio ) <EOL> def load_certificate_request ( type , buffer ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( buffer , _text_type ) : <EOL> buffer = buffer . encode ( "<STR_LIT:ascii>" ) <EOL> bio = _new_mem_buf ( buffer ) <EOL> if type == FILETYPE_PEM : <EOL> req = _lib . PEM_read_bio_X509_REQ ( bio , _ffi . NULL , _ffi . NULL , _ffi . NULL ) <EOL> elif type == FILETYPE_ASN1 : <EOL> req = _lib . d2i_X509_REQ_bio ( bio , _ffi . NULL ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if req == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> x509req = X509Req . __new__ ( X509Req ) <EOL> x509req . _req = _ffi . gc ( req , _lib . X509_REQ_free ) <EOL> return x509req <EOL> def sign ( pkey , data , digest ) : <EOL> """<STR_LIT>""" <EOL> data = _text_to_bytes_and_warn ( "<STR_LIT:data>" , data ) <EOL> digest_obj = _lib . EVP_get_digestbyname ( _byte_string ( digest ) ) <EOL> if digest_obj == _ffi . NULL : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> md_ctx = _ffi . new ( "<STR_LIT>" ) <EOL> md_ctx = _ffi . gc ( md_ctx , _lib . EVP_MD_CTX_cleanup ) <EOL> _lib . EVP_SignInit ( md_ctx , digest_obj ) <EOL> _lib . EVP_SignUpdate ( md_ctx , data , len ( data ) ) <EOL> pkey_length = ( PKey . bits ( pkey ) + <NUM_LIT:7> ) // <NUM_LIT:8> <EOL> signature_buffer = _ffi . new ( "<STR_LIT>" , pkey_length ) <EOL> signature_length = _ffi . new ( "<STR_LIT>" ) <EOL> final_result = _lib . EVP_SignFinal ( <EOL> md_ctx , signature_buffer , signature_length , pkey . _pkey ) <EOL> if final_result != <NUM_LIT:1> : <EOL> _raise_current_error ( ) <EOL> return _ffi . buffer ( signature_buffer , signature_length [ <NUM_LIT:0> ] ) [ : ] <EOL> def verify ( cert , signature , data , digest ) : <EOL> """<STR_LIT>""" <EOL> data = _text_to_bytes_and_warn ( "<STR_LIT:data>" , data ) <EOL> digest_obj = _lib . EVP_get_digestbyname ( _byte_string ( digest ) ) <EOL> if digest_obj == _ffi . NULL : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> pkey = _lib . X509_get_pubkey ( cert . _x509 ) <EOL> if pkey == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> pkey = _ffi . gc ( pkey , _lib . EVP_PKEY_free ) <EOL> md_ctx = _ffi . new ( "<STR_LIT>" ) <EOL> md_ctx = _ffi . gc ( md_ctx , _lib . EVP_MD_CTX_cleanup ) <EOL> _lib . EVP_VerifyInit ( md_ctx , digest_obj ) <EOL> _lib . EVP_VerifyUpdate ( md_ctx , data , len ( data ) ) <EOL> verify_result = _lib . EVP_VerifyFinal ( <EOL> md_ctx , signature , len ( signature ) , pkey <EOL> ) <EOL> if verify_result != <NUM_LIT:1> : <EOL> _raise_current_error ( ) <EOL> def dump_crl ( type , crl ) : <EOL> """<STR_LIT>""" <EOL> bio = _new_mem_buf ( ) <EOL> if type == FILETYPE_PEM : <EOL> ret = _lib . PEM_write_bio_X509_CRL ( bio , crl . _crl ) <EOL> elif type == FILETYPE_ASN1 : <EOL> ret = _lib . i2d_X509_CRL_bio ( bio , crl . _crl ) <EOL> elif type == FILETYPE_TEXT : <EOL> ret = _lib . X509_CRL_print ( bio , crl . _crl ) <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> assert ret == <NUM_LIT:1> <EOL> return _bio_to_string ( bio ) <EOL> def load_crl ( type , buffer ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( buffer , _text_type ) : <EOL> buffer = buffer . encode ( "<STR_LIT:ascii>" ) <EOL> bio = _new_mem_buf ( buffer ) <EOL> if type == FILETYPE_PEM : <EOL> crl = _lib . PEM_read_bio_X509_CRL ( bio , _ffi . NULL , _ffi . NULL , _ffi . NULL ) <EOL> elif type == FILETYPE_ASN1 : <EOL> crl = _lib . d2i_X509_CRL_bio ( bio , _ffi . NULL ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if crl == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> result = CRL . __new__ ( CRL ) <EOL> result . _crl = crl <EOL> return result <EOL> def load_pkcs7_data ( type , buffer ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( buffer , _text_type ) : <EOL> buffer = buffer . encode ( "<STR_LIT:ascii>" ) <EOL> bio = _new_mem_buf ( buffer ) <EOL> if type == FILETYPE_PEM : <EOL> pkcs7 = _lib . PEM_read_bio_PKCS7 ( bio , _ffi . NULL , _ffi . NULL , _ffi . NULL ) <EOL> elif type == FILETYPE_ASN1 : <EOL> pkcs7 = _lib . d2i_PKCS7_bio ( bio , _ffi . NULL ) <EOL> else : <EOL> _raise_current_error ( ) <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if pkcs7 == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> pypkcs7 = PKCS7 . __new__ ( PKCS7 ) <EOL> pypkcs7 . _pkcs7 = _ffi . gc ( pkcs7 , _lib . PKCS7_free ) <EOL> return pypkcs7 <EOL> def load_pkcs12 ( buffer , passphrase = None ) : <EOL> """<STR_LIT>""" <EOL> passphrase = _text_to_bytes_and_warn ( "<STR_LIT>" , passphrase ) <EOL> if isinstance ( buffer , _text_type ) : <EOL> buffer = buffer . encode ( "<STR_LIT:ascii>" ) <EOL> bio = _new_mem_buf ( buffer ) <EOL> if not passphrase : <EOL> passphrase = _ffi . NULL <EOL> p12 = _lib . d2i_PKCS12_bio ( bio , _ffi . NULL ) <EOL> if p12 == _ffi . NULL : <EOL> _raise_current_error ( ) <EOL> p12 = _ffi . gc ( p12 , _lib . PKCS12_free ) <EOL> pkey = _ffi . new ( "<STR_LIT>" ) <EOL> cert = _ffi . new ( "<STR_LIT>" ) <EOL> cacerts = _ffi . new ( "<STR_LIT>" ) <EOL> parse_result = _lib . PKCS12_parse ( p12 , passphrase , pkey , cert , cacerts ) <EOL> if not parse_result : <EOL> _raise_current_error ( ) <EOL> cacerts = _ffi . gc ( cacerts [ <NUM_LIT:0> ] , _lib . sk_X509_free ) <EOL> try : <EOL> _raise_current_error ( ) <EOL> except Error : <EOL> pass <EOL> if pkey [ <NUM_LIT:0> ] == _ffi . NULL : <EOL> pykey = None <EOL> else : <EOL> pykey = PKey . __new__ ( PKey ) <EOL> pykey . _pkey = _ffi . gc ( pkey [ <NUM_LIT:0> ] , _lib . EVP_PKEY_free ) <EOL> if cert [ <NUM_LIT:0> ] == _ffi . NULL : <EOL> pycert = None <EOL> friendlyname = None <EOL> else : <EOL> pycert = X509 . __new__ ( X509 ) <EOL> pycert . _x509 = _ffi . gc ( cert [ <NUM_LIT:0> ] , _lib . X509_free ) <EOL> friendlyname_length = _ffi . new ( "<STR_LIT>" ) <EOL> friendlyname_buffer = _lib . X509_alias_get0 ( <EOL> cert [ <NUM_LIT:0> ] , friendlyname_length <EOL> ) <EOL> friendlyname = _ffi . buffer ( <EOL> friendlyname_buffer , friendlyname_length [ <NUM_LIT:0> ] <EOL> ) [ : ] <EOL> if friendlyname_buffer == _ffi . NULL : <EOL> friendlyname = None <EOL> pycacerts = [ ] <EOL> for i in range ( _lib . sk_X509_num ( cacerts ) ) : <EOL> pycacert = X509 . __new__ ( X509 ) <EOL> pycacert . _x509 = _lib . sk_X509_value ( cacerts , i ) <EOL> pycacerts . append ( pycacert ) <EOL> if not pycacerts : <EOL> pycacerts = None <EOL> pkcs12 = PKCS12 . __new__ ( PKCS12 ) <EOL> pkcs12 . _pkey = pykey <EOL> pkcs12 . _cert = pycert <EOL> pkcs12 . _cacerts = pycacerts <EOL> pkcs12 . _friendlyname = friendlyname <EOL> return pkcs12 <EOL> _lib . OpenSSL_add_all_algorithms ( ) <EOL> _lib . SSL_load_error_strings ( ) <EOL> _lib . ASN1_STRING_set_default_mask_asc ( b'<STR_LIT>' ) </s>
