<s> from __future__ import absolute_import , division , print_function <EOL> import abc <EOL> import base64 <EOL> import struct <EOL> from enum import Enum <EOL> import six <EOL> from cryptography import utils <EOL> from cryptography . exceptions import UnsupportedAlgorithm <EOL> from cryptography . hazmat . primitives . asymmetric import dsa , ec , rsa <EOL> def load_pem_private_key ( data , password , backend ) : <EOL> return backend . load_pem_private_key ( data , password ) <EOL> def load_pem_public_key ( data , backend ) : <EOL> return backend . load_pem_public_key ( data ) <EOL> def load_der_private_key ( data , password , backend ) : <EOL> return backend . load_der_private_key ( data , password ) <EOL> def load_der_public_key ( data , backend ) : <EOL> return backend . load_der_public_key ( data ) <EOL> def load_ssh_public_key ( data , backend ) : <EOL> key_parts = data . split ( b'<STR_LIT:U+0020>' , <NUM_LIT:2> ) <EOL> if len ( key_parts ) < <NUM_LIT:2> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' ) <EOL> key_type = key_parts [ <NUM_LIT:0> ] <EOL> if key_type == b'<STR_LIT>' : <EOL> loader = _load_ssh_rsa_public_key <EOL> elif key_type == b'<STR_LIT>' : <EOL> loader = _load_ssh_dss_public_key <EOL> elif key_type in [ <EOL> b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' , <EOL> ] : <EOL> loader = _load_ssh_ecdsa_public_key <EOL> else : <EOL> raise UnsupportedAlgorithm ( '<STR_LIT>' ) <EOL> key_body = key_parts [ <NUM_LIT:1> ] <EOL> try : <EOL> decoded_data = base64 . b64decode ( key_body ) <EOL> except TypeError : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> inner_key_type , rest = _read_next_string ( decoded_data ) <EOL> if inner_key_type != key_type : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> return loader ( key_type , rest , backend ) <EOL> def _load_ssh_rsa_public_key ( key_type , decoded_data , backend ) : <EOL> e , rest = _read_next_mpint ( decoded_data ) <EOL> n , rest = _read_next_mpint ( rest ) <EOL> if rest : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return rsa . RSAPublicNumbers ( e , n ) . public_key ( backend ) <EOL> def _load_ssh_dss_public_key ( key_type , decoded_data , backend ) : <EOL> p , rest = _read_next_mpint ( decoded_data ) <EOL> q , rest = _read_next_mpint ( rest ) <EOL> g , rest = _read_next_mpint ( rest ) <EOL> y , rest = _read_next_mpint ( rest ) <EOL> if rest : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> parameter_numbers = dsa . DSAParameterNumbers ( p , q , g ) <EOL> public_numbers = dsa . DSAPublicNumbers ( y , parameter_numbers ) <EOL> return public_numbers . public_key ( backend ) <EOL> def _load_ssh_ecdsa_public_key ( expected_key_type , decoded_data , backend ) : <EOL> curve_name , rest = _read_next_string ( decoded_data ) <EOL> data , rest = _read_next_string ( rest ) <EOL> if expected_key_type != b"<STR_LIT>" + curve_name : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if rest : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> curve = { <EOL> b"<STR_LIT>" : ec . SECP256R1 , <EOL> b"<STR_LIT>" : ec . SECP384R1 , <EOL> b"<STR_LIT>" : ec . SECP521R1 , <EOL> } [ curve_name ] ( ) <EOL> if six . indexbytes ( data , <NUM_LIT:0> ) != <NUM_LIT:4> : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> numbers = ec . EllipticCurvePublicNumbers . from_encoded_point ( curve , data ) <EOL> return numbers . public_key ( backend ) <EOL> def _read_next_string ( data ) : <EOL> """<STR_LIT>""" <EOL> str_len , = struct . unpack ( '<STR_LIT>' , data [ : <NUM_LIT:4> ] ) <EOL> return data [ <NUM_LIT:4> : <NUM_LIT:4> + str_len ] , data [ <NUM_LIT:4> + str_len : ] <EOL> def _read_next_mpint ( data ) : <EOL> """<STR_LIT>""" <EOL> mpint_data , rest = _read_next_string ( data ) <EOL> return ( <EOL> utils . int_from_bytes ( mpint_data , byteorder = '<STR_LIT>' , signed = False ) , rest <EOL> ) <EOL> class Encoding ( Enum ) : <EOL> PEM = "<STR_LIT>" <EOL> DER = "<STR_LIT>" <EOL> class PrivateFormat ( Enum ) : <EOL> PKCS8 = "<STR_LIT>" <EOL> TraditionalOpenSSL = "<STR_LIT>" <EOL> class PublicFormat ( Enum ) : <EOL> SubjectPublicKeyInfo = "<STR_LIT>" <EOL> PKCS1 = "<STR_LIT>" <EOL> @ six . add_metaclass ( abc . ABCMeta ) <EOL> class KeySerializationEncryption ( object ) : <EOL> pass <EOL> @ utils . register_interface ( KeySerializationEncryption ) <EOL> class BestAvailableEncryption ( object ) : <EOL> def __init__ ( self , password ) : <EOL> if not isinstance ( password , bytes ) or len ( password ) == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . password = password <EOL> @ utils . register_interface ( KeySerializationEncryption ) <EOL> class NoEncryption ( object ) : <EOL> pass </s>
