def _load_key(key_object, container):
    """
    Loads a certificate, public key or private key into a Certificate,
    PublicKey or PrivateKey object

    :param key_object:
        An asn1crypto.x509.Certificate, asn1crypto.keys.PublicKeyInfo or
        asn1crypto.keys.PrivateKeyInfo object

    :param container:
        The class of the object to hold the key_handle

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        oscrypto.errors.AsymmetricKeyError - when the key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A PrivateKey, PublicKey or Certificate object, based on container
    """

    key_info = key_object
    if isinstance(key_object, x509.Certificate):
        key_info = key_object['tbs_certificate']['subject_public_key_info']

    algo = key_info.algorithm
    curve_name = None

    if algo == 'ec':
        curve_type, curve_name = key_info.curve
        if curve_type != 'named':
            raise AsymmetricKeyError(pretty_message(
                '''
                Windows only supports EC keys using named curves
                '''
            ))
        if curve_name not in set(['secp256r1', 'secp384r1', 'secp521r1']):
            raise AsymmetricKeyError(pretty_message(
                '''
                Windows only supports EC keys using the named curves
                secp256r1, secp384r1 and secp521r1
                '''
            ))

    elif algo == 'dsa':
        if key_info.hash_algo is None:
            raise IncompleteAsymmetricKeyError(pretty_message(
                '''
                The DSA key does not contain the necessary p, q and g
                parameters and can not be used
                '''
            ))
        elif key_info.bit_size > 1024 and (_win_version_info < (6, 2) or _backend == 'winlegacy'):
            raise AsymmetricKeyError(pretty_message(
                '''
                Windows XP, 2003, Vista, 7 and Server 2008 only support DSA
                keys based on SHA1 (1024 bits or less) - this key is based
                on %s and is %s bits
                ''',
                key_info.hash_algo.upper(),
                key_info.bit_size
            ))
        elif key_info.bit_size == 2048 and key_info.hash_algo == 'sha1':
            raise AsymmetricKeyError(pretty_message(
                '''
                Windows only supports 2048 bit DSA keys based on SHA2 - this
                key is 2048 bits and based on SHA1, a non-standard
                combination that is usually generated by old versions
                of OpenSSL
                '''
            ))

    if _backend == 'winlegacy':
        if algo == 'ec':
            return container(None, key_object)
        return _advapi32_load_key(key_object, key_info, container)
    return _bcrypt_load_key(key_object, key_info, container, curve_name)
def _load_key(key_object, container):
    """
    Loads a certificate, public key or private key into a Certificate,
    PublicKey or PrivateKey object

    :param key_object:
        An asn1crypto.x509.Certificate, asn1crypto.keys.PublicKeyInfo or
        asn1crypto.keys.PrivateKeyInfo object

    :param container:
        The class of the object to hold the key_handle

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        oscrypto.errors.AsymmetricKeyError - when the key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A PrivateKey, PublicKey or Certificate object, based on container
    """

    key_info = key_object
    if isinstance(key_object, x509.Certificate):
        key_info = key_object['tbs_certificate']['subject_public_key_info']

    algo = key_info.algorithm
    curve_name = None

    if algo == 'ec':
        curve_type, curve_name = key_info.curve
        if curve_type != 'named':
            raise AsymmetricKeyError(pretty_message(
                '''
                Windows only supports EC keys using named curves
                '''
            ))
        if curve_name not in set(['secp256r1', 'secp384r1', 'secp521r1']):
            raise AsymmetricKeyError(pretty_message(
                '''
                Windows only supports EC keys using the named curves
                secp256r1, secp384r1 and secp521r1
                '''
            ))

    elif algo == 'dsa':
        if key_info.hash_algo is None:
            raise IncompleteAsymmetricKeyError(pretty_message(
                '''
                The DSA key does not contain the necessary p, q and g
                parameters and can not be used
                '''
            ))
        elif key_info.bit_size > 1024 and (_win_version_info < (6, 2) or _backend == 'winlegacy'):
            raise AsymmetricKeyError(pretty_message(
                '''
                Windows XP, 2003, Vista, 7 and Server 2008 only support DSA
                keys based on SHA1 (1024 bits or less) - this key is based
                on %s and is %s bits
                ''',
                key_info.hash_algo.upper(),
                key_info.bit_size
            ))
        elif key_info.bit_size == 2048 and key_info.hash_algo == 'sha1':
            raise AsymmetricKeyError(pretty_message(
                '''
                Windows only supports 2048 bit DSA keys based on SHA2 - this
                key is 2048 bits and based on SHA1, a non-standard
                combination that is usually generated by old versions
                of OpenSSL
                '''
            ))

    if _backend == 'winlegacy':
        if algo == 'ec':
            return container(None, key_object)
        return _advapi32_load_key(key_object, key_info, container)
    return _bcrypt_load_key(key_object, key_info, container, curve_name)
def _load_key ( key_object , container ) : key_info = key_object if isinstance ( key_object , x509 . Certificate ) : key_info = key_object [ 'tbs_certificate' ] [ 'subject_public_key_info' ] algo = key_info . algorithm curve_name = None if algo == 'ec' : curve_type , curve_name = key_info . curve if curve_type != 'named' : raise AsymmetricKeyError ( pretty_message ( '''
                Windows only supports EC keys using named curves
                ''' ) ) if curve_name not in set ( [ 'secp256r1' , 'secp384r1' , 'secp521r1' ] ) : raise AsymmetricKeyError ( pretty_message ( '''
                Windows only supports EC keys using the named curves
                secp256r1, secp384r1 and secp521r1
                ''' ) ) elif algo == 'dsa' : if key_info . hash_algo is None : raise IncompleteAsymmetricKeyError ( pretty_message ( '''
                The DSA key does not contain the necessary p, q and g
                parameters and can not be used
                ''' ) ) elif key_info . bit_size > 1024 and ( _win_version_info < ( 6 , 2 ) or _backend == 'winlegacy' ) : raise AsymmetricKeyError ( pretty_message ( '''
                Windows XP, 2003, Vista, 7 and Server 2008 only support DSA
                keys based on SHA1 (1024 bits or less) - this key is based
                on %s and is %s bits
                ''' , key_info . hash_algo . upper ( ) , key_info . bit_size ) ) elif key_info . bit_size == 2048 and key_info . hash_algo == 'sha1' : raise AsymmetricKeyError ( pretty_message ( '''
                Windows only supports 2048 bit DSA keys based on SHA2 - this
                key is 2048 bits and based on SHA1, a non-standard
                combination that is usually generated by old versions
                of OpenSSL
                ''' ) ) if _backend == 'winlegacy' : if algo == 'ec' : return container ( None , key_object ) return _advapi32_load_key ( key_object , key_info , container ) return _bcrypt_load_key ( key_object , key_info , container , curve_name )
Loads a certificate, public key or private key into a Certificate,
    PublicKey or PrivateKey object

    :param key_object:
        An asn1crypto.x509.Certificate, asn1crypto.keys.PublicKeyInfo or
        asn1crypto.keys.PrivateKeyInfo object

    :param container:
        The class of the object to hold the key_handle

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        oscrypto.errors.AsymmetricKeyError - when the key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A PrivateKey, PublicKey or Certificate object, based on container
Loads a certificate public key or private key into a Certificate PublicKey or PrivateKey object