HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.60.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 5 05:00:59 EST 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //proc/self/root/var/opt/nydus/ops/oscrypto/_openssl/__pycache__/asymmetric.cpython-39.pyc
a

�,�h;��@sddlmZmZmZmZddlZddlmZm	Z	m
Z
mZmZm
Z
ddlmZmZmZmZmZmZmZmZmZddlmZddlmZmZmZmZmZm Z m!Z!m"Z"m#Z#dd	l$m%Z%m&Z&m'Z'm(Z(dd
l)m*Z*m+Z+m,Z,ddl-m.Z.m/Z/m0Z0m1Z1ddl2m3Z3gd
�Z4Gdd�de�Z5Gdd�de�Z6Gdd�de�ZdIdd�Z7dd�Z8dd�Z9dd�Z:dJdd�Z;dd�Z<d d!�Z=dKd"d#�Z>dLd$d%�Z?d&d'�Z@d(d)�ZAd*d+�ZBd,d-�ZCd.d/�ZDd0d1�ZEd2d3�ZFd4d5�ZGd6d7�ZHd8d9�ZId:d;�ZJdMd=d>�ZKd?d@�ZLdAdB�ZMdCdD�ZNdEdF�ZOdNdGdH�ZPdS)O�)�unicode_literals�division�absolute_import�print_functionN�)�Certificate�DHParameters�ECDomainParameters�PrivateKeyInfo�PublicKeyAlgorithm�
PublicKeyInfo)	�_CertificateBase�_fingerprint�
_parse_pkcs12�_PrivateKeyBase�_PublicKeyBase�_unwrap_private_key_info�parse_certificate�
parse_private�parse_public)�pretty_message)	�buffer_from_bytes�buffer_pointer�bytes_from_buffer�deref�is_null�new�null�unwrap�write_to_buffer�)�	libcrypto�LibcryptoConst�libcrypto_version_info�handle_openssl_error)�AsymmetricKeyError�IncompleteAsymmetricKeyError�SignatureError)�	type_name�str_cls�byte_cls�	int_types)�constant_compare)r�dsa_sign�
dsa_verify�
ecdsa_sign�ecdsa_verify�
generate_pair�load_certificate�load_pkcs12�load_private_key�load_public_key�parse_pkcs12�
PrivateKey�	PublicKey�rsa_oaep_decrypt�rsa_oaep_encrypt�rsa_pkcs1v15_decrypt�rsa_pkcs1v15_encrypt�rsa_pkcs1v15_sign�rsa_pkcs1v15_verify�rsa_pss_sign�rsa_pss_verifyc@sDeZdZdZdZdZdZdd�Zedd��Z	edd��Z
d	d
�ZdS)r7zC
    Container for the OpenSSL representation of a private key
    NcCs||_||_t|_dS)z�
        :param evp_pkey:
            An OpenSSL EVP_PKEY value from loading/importing the key

        :param asn1:
            An asn1crypto.keys.PrivateKeyInfo object
        N��evp_pkey�asn1r!�_lib��selfrBrC�rG�M/opt/nydus/tmp/pip-target-wkfpz8uv/lib/python/oscrypto/_openssl/asymmetric.py�__init__Ps	zPrivateKey.__init__c
Cs�|jdur�t�|jt��}t|�}t|�}t�|j|�}t|�t||�}t	�
|�}tdkr�|jdkr�|�
�}d|dd<|��}t||�t|�}t�t�t|�|�}	t|	�r�td�t|	|�|_|jS)z\
        :return:
            A PublicKey object corresponding to this private key.
        N���
rsassa_pss�rsa�	algorithmr)�_public_keyr!Z
i2d_PUBKEYrBrrrr$rr�loadr#rN�copy�dumpr�len�
d2i_PUBKEYrr8)
rF�buffer_sizeZ
pubkey_bufferZpubkey_pointerZ
pubkey_lengthZpubkey_datarCZ	temp_asn1Z	temp_dataZpub_evp_pkeyrGrGrH�
public_key]s&



zPrivateKey.public_keycCs|jdurt|jt�|_|jS)aY
        Creates a fingerprint that can be compared with a public key to see if
        the two form a pair.

        This fingerprint is not compatible with fingerprints generated by any
        other software.

        :return:
            A byte string that is a sha256 hash of selected components (based
            on the key type)
        N)rrCr4�rFrGrGrH�fingerprints
zPrivateKey.fingerprintcCs$|jr |j�|j�d|_d|_dS�N�rBrDZ
EVP_PKEY_freerWrGrGrH�__del__�szPrivateKey.__del__)�__name__�
__module__�__qualname__�__doc__rBrOrDrI�propertyrVrXr[rGrGrGrHr7Ds

!
r7c@s(eZdZdZdZdZdd�Zdd�ZdS)r8zB
    Container for the OpenSSL representation of a public key
    NcCs||_||_t|_dS)z�
        :param evp_pkey:
            An OpenSSL EVP_PKEY value from loading/importing the key

        :param asn1:
            An asn1crypto.keys.PublicKeyInfo object
        NrArErGrGrHrI�s	zPublicKey.__init__cCs$|jr |j�|j�d|_d|_dSrYrZrWrGrGrHr[�szPublicKey.__del__)r\r]r^r_rBrDrIr[rGrGrGrHr8�s

r8c@sTeZdZdZdZdZdZdZdd�Ze	dd��Z
e	dd��Ze	d	d
��Zdd�Z
dS)
rzC
    Container for the OpenSSL representation of a certificate
    NcCs||_||_t|_dS)z�
        :param x509:
            An OpenSSL X509 value from loading/importing the certificate

        :param asn1:
            An asn1crypto.x509.Certificate object
        N)�x509rCr!rD)rFrarCrGrGrHrI�s	zCertificate.__init__cCs|jjS)z_
        :return:
            The EVP_PKEY of the public key this certificate contains
        )rVrBrWrGrGrHrB�szCertificate.evp_pkeycCsT|jsN|jrNtdkr2|jjjdkr2t|jj�|_nt�|j�}t	||jj�|_|jS)zh
        :return:
            The PublicKey object for the public key this certificate contains
        rJrL)
rOrar#rCrVrNr5r!ZX509_get_pubkeyr8)rFrBrGrGrHrV�szCertificate.public_keycCs�|jdur�d|_|jjtddg�vr�|jdj}|jdj}|dkrJt}n8|dkrXt}n*|dkrft}n|d	krtt	}nt
td
|���z,||j|jdj
|jd��|�d
|_Wnty�Yn0|jS)zT
        :return:
            A boolean - if the certificate is self-signed
        NF�yes�maybeZsignature_algorithmZrsassa_pkcs1v15rL�dsaZecdsaz�
                        Unable to verify the signature of the certificate since
                        it uses the unsupported algorithm %s
                        Zsignature_valueZtbs_certificateT)�_self_signedrC�self_signed�set�signature_algo�	hash_algor>r@r.r0�OSErrorrrVZnativerRr')rFrhriZverify_funcrGrGrHrf�s8
�
�
zCertificate.self_signedcCs:|jr|j��d|_|jr6|j�|j�d|_d|_dSrY)rOr[rarDZ	X509_freerWrGrGrHr[s
zCertificate.__del__)r\r]r^r_rarOrerDrIr`rBrVrfr[rGrGrGrHr�s



+rc	Cs�|tgd��vr"ttdt|����|dkrN|tgd��vr�ttdt|����nz|dkr�tdkrz|dkr�ttd	t|����q�|tgd
��vr�ttdt|����n*|dkr�|tgd
��vr�ttdt|����|dk�r(d}d}�z$t��}t|�r�td�t	td�}t�
|d�}t|�t|�}t�|||t
��}t|�t�|t
��}|dk�rVt|�t|�}t�|t|��}|dk�r�t|�t||�}	t�|t
��}|dk�r�t|�t|�}t�|t|��}|dk�r�t|�t||�}
W|�r�t�|�|�r$t�|�n"|�rt�|�|�r"t�|�0�n�|dk�r^d}�zt��}t|��rTtd�t�||t
�dt
�t
�t
��}t|�t�|�}t|�t�|t
��}|dk�r�t|�t|�}t�|t|��}|dk�r�t|�t||�}	t�|t
��}|dk�rt|�t|�}t�|t|��}|dk�r*t|�t||�}
W|�rZt�|�n|�rXt�|�0�nZ|dk�r�d}�z6tjtjtjd
�|}
t�|
�}t|��r�td�t� |�}t|�t�!|tj"�t�#|t
��}|dk�r�t|�t|�}t�#|t|��}|dk�rt|�t||�}t$t%dt&d|d�d��|d��}|�'�}	t�(|t
��}|dk�r^t|�t|�}t�(|t|��}|dk�r�t|�t||�}
W|�r�t�)|�n|�r�t�)|�0t*|	�t+|
�fS)aP
    Generates a public/private key pair

    :param algorithm:
        The key algorithm - "rsa", "dsa" or "ec"

    :param bit_size:
        An integer - used for "rsa" and "dsa". For "rsa" the value maye be 1024,
        2048, 3072 or 4096. For "dsa" the value may be 1024, plus 2048 or 3072
        if OpenSSL 1.0.0 or newer is available.

    :param curve:
        A unicode string - used for "ec" keys. Valid values include "secp256r1",
        "secp384r1" and "secp521r1".

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A 2-element tuple of (PublicKey, PrivateKey). The contents of each key
        may be saved by calling .asn1.dump().
    )rMrd�eczM
            algorithm must be one of "rsa", "dsa", "ec", not %s
            rM)����zX
                bit_size must be one of 1024, 2048, 3072, 4096, not %s
                rd�r rlzG
                    bit_size must be 1024, not %s
                    )rlrmrnzZ
                    bit_size must be one of 1024, 2048, 3072, not %s
                    rk)Z	secp256r1Z	secp384r1Z	secp521r1zt
                curve must be one of "secp256r1", "secp384r1", "secp521r1",
                not %s
                Nrz	BIGNUM **s65537�named)�name�value)rN�
parameters)rNrV),rg�
ValueErrorr�reprr#r!ZRSA_newrr$rZ	BN_dec2bnrZRSA_generate_key_exrZi2d_RSAPublicKeyrrrZi2d_RSAPrivateKey�RSA_freeZBN_freeZDSA_newZDSA_generate_parameters_exZDSA_generate_keyZi2d_DSA_PUBKEYZi2d_DSAPrivateKey�DSA_freer"ZNID_X9_62_prime256v1Z
NID_secp384r1Z
NID_secp521r1ZEC_KEY_new_by_curve_nameZEC_KEY_generate_keyZEC_KEY_set_asn1_flagZOPENSSL_EC_NAMED_CURVEZi2o_ECPublicKeyrrr	rRZi2d_ECPrivateKey�EC_KEY_freer5r4)rN�bit_sizeZcurverM�exponentZexponent_pointer�result�
buffer_length�bufferZpublic_key_bytesZprivate_key_bytesrd�ec_keyZcurve_idZpublic_key_point_bytesrVrGrGrHr1#s�����







�








�
��





���


�r1c	Cst|t�sttdt|����|dkr,td��|dkr<td��|ddkrPtd��d	}z�t��}t|�rnt	d�t�
||tjt
��}t	|�t�|t
��}|dkr�t	|�t|�}t�|t|��}|dkr�t	|�t||�}t�|�W|r�t�|�Sn|�rt�|�0d	S)
a`
    Generates DH parameters for use with Diffie-Hellman key exchange. Returns
    a structure in the format of DHParameter defined in PKCS#3, which is also
    used by the OpenSSL dhparam tool.

    THIS CAN BE VERY TIME CONSUMING!

    :param bit_size:
        The integer bit size of the parameters to generate. Must be between 512
        and 4096, and divisible by 64. Recommended secure value as of early 2016
        is 2048, with an absolute minimum of 1024.

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        An asn1crypto.algos.DHParameters object. Use
        oscrypto.asymmetric.dump_dh_parameters() to save to disk for usage with
        web servers.
    z=
            bit_size must be an integer, not %s
            iz-bit_size must be greater than or equal to 512roz+bit_size must be less than or equal to 4096�@rz!bit_size must be a multiple of 64N)�
isinstancer+�	TypeErrorrr(rur!ZDH_newrr$ZDH_generate_parameters_exr"ZDH_GENERATOR_2rZi2d_DHparamsrrrrrPZDH_free)rzZdhr|r}r~Zdh_params_bytesrGrGrH�generate_dh_parameters�sD
�


��r�cCs�t|t�r|}nht|t�r$t|�}nTt|t�rft|d��}t|���}Wd�qx1sZ0Ynttdt	|����t
|�S)a�
    Loads an x509 certificate into a Certificate object

    :param source:
        A byte string of file contents, a unicode string filename or an
        asn1crypto.x509.Certificate object

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A Certificate object
    �rbNz�
            source must be a byte string, unicode string or
            asn1crypto.x509.Certificate object, not %s
            )r��Asn1Certificater*rr)�open�readr�rr(�
_load_x509)�source�certificate�frGrGrHr24s



,�r2cCsB|��}t|�}t�t�t|�t|��}t|�r8td�t	||�S)z�
    Loads an ASN.1 object of an x509 certificate into a Certificate object

    :param certificate:
        An asn1crypto.x509.Certificate object

    :return:
        A Certificate object
    r)
rRrr!Zd2i_X509rrrSrr$r)r�r�r~rBrGrGrHr�[sr�cCs�t|t�r|}n�|durHt|t�r,|�d�}t|t�sHttdt|����t|t�r�t|d��}|�	�}Wd�q�1sz0Ynt|t�s�ttdt|����t
||�}t|�S)a
    Loads a private key into a PrivateKey object

    :param source:
        A byte string of file contents, a unicode string filename or an
        asn1crypto.keys.PrivateKeyInfo object

    :param password:
        A byte or unicode string to decrypt the private key file. Unicode
        strings will be encoded using UTF-8. Not used is the source is a
        PrivateKeyInfo object.

    :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 private key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A PrivateKey object
    N�utf-8zP
                    password must be a byte string, not %s
                    r�z�
                source must be a byte string, unicode string or
                asn1crypto.keys.PrivateKeyInfo object, not %s
                )r�r
r)�encoder*r�rr(r�r�r�	_load_key)r��password�private_objectr�rGrGrHr4os(



�
(
�
r4cCs0t|t�r|}nht|t�r$t|�}nTt|t�rft|d��}t|���}Wd�qx1sZ0Ynttdt	|����|j
dkr�tdkr�|jdkr�t
td|j���n|jdur�ttd���td	kr�|j
d
kr�|��}d|dd<|��}n|��}t|�}t�t�t|�t|��}t|��r&td
�t||�S)a3
    Loads a public key into a PublicKey object

    :param source:
        A byte string of file contents, a unicode string filename or an
        asn1crypto.keys.PublicKeyInfo object

    :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 public key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A PublicKey object
    r�Nz�
            source must be a byte string, unicode string or
            asn1crypto.keys.PublicKeyInfo object, not %s
            rdrp�sha2z�
                OpenSSL 0.9.8 only supports DSA keys based on SHA1 (2048 bits or
                less) - this key is based on SHA2 and is %s bits
                z�
                The DSA key does not contain the necessary p, q and g
                parameters and can not be used
                rJrLrMrNr)r�rr*rr)r�r�r�rr(rNr#rir%rzr&rQrRrr!rTrrrSrr$r8)r�rVr�Ztemp_key�datar~rBrGrGrHr5�s>



,�
�
�	

r5cCsrtdkr,|jdkr,|jdkr,ttd|j���t|���}t|�}t	�
t�t|�t
|��}t|�rhtd�t||�S)z�
    Loads a private key into a PrivateKey object

    :param private_object:
        An asn1crypto.keys.PrivateKeyInfo object

    :return:
        A PrivateKey object
    rprdr�z�
            OpenSSL 0.9.8 only supports DSA keys based on SHA1 (2048 bits or
            less) - this key is based on SHA2 and is %s bits
            r)r#rNrir%rrzrrRrr!Zd2i_AutoPrivateKeyrrrSrr$r7)r�r�r~rBrGrGrHr��s�r�cCst||t�S)a�
    Parses a PKCS#12 ANS.1 DER-encoded structure and extracts certs and keys

    :param data:
        A byte string of a DER-encoded PKCS#12 file

    :param password:
        A byte string of the password to any encrypted data

    :raises:
        ValueError - when any of the parameters are of the wrong type or value
        OSError - when an error is returned by one of the OS decryption functions

    :return:
        A three-element tuple of:
         1. An asn1crypto.keys.PrivateKeyInfo object
         2. An asn1crypto.x509.Certificate object
         3. A list of zero or more asn1crypto.x509.Certificate objects that are
            "extra" certificates, possibly intermediates from the cert chain
    )rr4)r�r�rGrGrHr6
sr6c	Cs�|dur8t|t�r|�d�}t|t�s8ttdt|����t|t�rvt|d��}|��}Wd�q�1sj0Ynt|t�s�ttdt|����t	||�\}}}d}d}|r�t
|�}|r�t|�}dd�|D�}|||fS)a�
    Loads a .p12 or .pfx file into a PrivateKey object and one or more
    Certificates objects

    :param source:
        A byte string of file contents or a unicode string filename

    :param password:
        A byte or unicode string to decrypt the PKCS12 file. Unicode strings
        will be encoded using UTF-8.

    :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 a contained key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A three-element tuple containing (PrivateKey, Certificate, [Certificate, ...])
    Nr�zH
                password must be a byte string, not %s
                r�zR
            source must be a byte string or a unicode string, not %s
            cSsg|]}t|��qSrG)r�)�.0�inforGrGrH�
<listcomp>[�zload_pkcs12.<locals>.<listcomp>)r�r)r�r*r�rr(r�r�r6r�r�)	r�r�r�Zkey_infoZ	cert_infoZextra_certs_info�key�certZextra_certsrGrGrHr3#s2


�
(
�r3cCst||tj�S)aF
    Encrypts a byte string using an RSA public key or certificate. Uses PKCS#1
    v1.5 padding.

    :param certificate_or_public_key:
        A PublicKey or Certificate object

    :param data:
        A byte string, with a maximum length 11 bytes less than the key length
        (in bytes)

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the encrypted data
    )�_encryptr"�RSA_PKCS1_PADDING��certificate_or_public_keyr�rGrGrHr<`sr<cCst||tj�S)a�
    Decrypts a byte string using an RSA private key. Uses PKCS#1 v1.5 padding.

    :param private_key:
        A PrivateKey object

    :param ciphertext:
        A byte string of the encrypted data

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the original plaintext
    )�_decryptr"r���private_key�
ciphertextrGrGrHr;xsr;cCst||tj�S)aZ
    Encrypts a byte string using an RSA public key or certificate. Uses PKCS#1
    OAEP padding with SHA1.

    :param certificate_or_public_key:
        A PublicKey or Certificate object

    :param data:
        A byte string, with a maximum length 41 bytes (or more) less than the
        key length (in bytes)

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the encrypted data
    )r�r"�RSA_PKCS1_OAEP_PADDINGr�rGrGrHr:�sr:cCst||tj�S)a�
    Decrypts a byte string using an RSA private key. Uses PKCS#1 OAEP padding
    with SHA1.

    :param private_key:
        A PrivateKey object

    :param ciphertext:
        A byte string of the encrypted data

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the original plaintext
    )r�r"r�r�rGrGrHr9�sr9cCstdkrt�|�St�|�S)z�
    Handles the function name change from OpenSSL 1.1 -> 3.0

    :param evp_pkey:
        The EVP_PKEY of the Certificte or PublicKey to get the size of

    :return:
        An int of the number of bytes necessary for the key
    rJ)r#r!Z
EVP_PKEY_sizeZEVP_PKEY_get_size)rBrGrGrH�_evp_pkey_get_size�s
r�c	Cs�t|ttf�s ttdt|����t|t�s<ttdt|����d}zXt|j�}t	|�}t
�|j�}t
�t
|�||||�}t|�t||�W|r�t
�|�Sn|r�t
�|�0dS)a%
    Encrypts plaintext using an RSA public key or certificate

    :param certificate_or_public_key:
        A PublicKey, Certificate or PrivateKey object

    :param data:
        The byte string to encrypt

    :param padding:
        The padding mode to use

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the encrypted data
    ��
            certificate_or_public_key must be an instance of the Certificate or
            PublicKey class, not %s
            �<
            data must be a byte string, not %s
            N)r�rr8r�rr(r*r�rBrr!�EVP_PKEY_get1_RSAZRSA_public_encryptrSr$rrw)r�r��paddingrMrUr~�resrGrGrHr��s2�
�


��r�c	Cs�t|t�sttdt|����t|t�s8ttdt|����d}zXt|j�}t|�}t	�
|j�}t	�t|�||||�}t
|�t||�W|r�t	�|�Sn|r�t	�|�0dS)a�
    Decrypts RSA ciphertext using a private key

    :param private_key:
        A PrivateKey object

    :param ciphertext:
        The ciphertext - a byte string

    :param padding:
        The padding mode to use

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the plaintext
    zY
            private_key must be an instance of the PrivateKey class, not %s
            zB
            ciphertext must be a byte string, not %s
            N)r�r7r�rr(r*r�rBrr!r�ZRSA_private_decryptrSr$rrw)r�r�r�rMrUr~r�rGrGrHr�s2
�
�


��r�cCs,|jdkrttd|j�����t||||�S)a�
    Verifies an RSASSA-PKCS-v1.5 signature.

    When the hash_algorithm is "raw", the operation is identical to RSA
    public key decryption. That is: the data is not hashed and no ASN.1
    structure with an algorithm identifier of the hash algorithm is placed in
    the encrypted byte string.

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to verify the signature with

    :param signature:
        A byte string of the signature to verify

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384",
        "sha512" or "raw"

    :raises:
        oscrypto.errors.SignatureError - when the signature is determined to be invalid
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library
    rM�L
            The key specified is not an RSA public key, but %s
            �rNrur�upper�_verify�r��	signaturer��hash_algorithmrGrGrHr><s
�r>cCs<|j}|dkr*|dkr*ttd|j�����t||||dd�S)a�
    Verifies an RSASSA-PSS signature. For the PSS padding the mask gen algorithm
    will be mgf1 using the same hash algorithm as the signature. The salt length
    with be the length of the hash algorithm, and the trailer field with be the
    standard 0xBC byte.

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to verify the signature with

    :param signature:
        A byte string of the signature to verify

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :raises:
        oscrypto.errors.SignatureError - when the signature is determined to be invalid
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library
    rMrLr�T��rsa_pss_paddingr�)r�r�r�r��cp_algrGrGrHr@ds�r@cCs,|jdkrttd|j�����t||||�S)a�
    Verifies a DSA signature

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to verify the signature with

    :param signature:
        A byte string of the signature to verify

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :raises:
        oscrypto.errors.SignatureError - when the signature is determined to be invalid
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library
    rdzK
            The key specified is not a DSA public key, but %s
            r�r�rGrGrHr.�s
�r.cCs,|jdkrttd|j�����t||||�S)a�
    Verifies an ECDSA signature

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to verify the signature with

    :param signature:
        A byte string of the signature to verify

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :raises:
        oscrypto.errors.SignatureError - when the signature is determined to be invalid
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library
    rkzK
            The key specified is not an EC public key, but %s
            r�r�rGrGrHr0�s
�r0Fc	Cs�t|ttf�s ttdt|����t|t�s<ttdt|����t|t�sXttdt|����|j}|dkpl|dk}tgd��}|r�|s�|tdg�O}||vr�d}|r�|s�|d	7}t	td
|t
|����|s�|r�t	td|�����|�r�|dk�r�t|�|j
dk�rt	td
|j
t|����d}	z�t�|j�}	t|	��r8td�t|j�}
t|
�}t�t|�|||	tj�}t|�t||�}
t||
��s�td��W|	�r�t�|	�dSn|	�r�t�|	�0d}d}	d}d}d}d}�z|tdk�r�t��}nt��}tjtj tj!tj"tj#tj$d�|�}tdk�r�|�r�|�r�t%t&|�|��'�}t�|j�}	t|	��rTtd�t|j�}
t|
�}t�t|�|||	tj(�}t|�t�)|	|||tj*�}�q�|�r�t�+||t,��}t|�t�-||t|��}t|�t�.||t|�|j�}�q�|dk�rvt%t&|�|��'�}t|�}t/|�}t�0t,�|t|��}t|��rBtd��t�1|j�}t|��r`td�t�2|t|�||�}n�|dk�r�t%t&|�|��'�}t|�}t/|�}t�3t,�|t|��}t|��r�td��t�4|j�}t|��r�td�t�5|t|�||�}n�t6td�}t�7|||t,�|j�}t|�t8|�}|�r�t�9|tj:dtj;tj<t,��}t|�tdk�r�t�9|tj:tj=tj>Btj?dt,��}t|�t�-||t|��}t|�t�@||t|��}|dk�r�td��t|�W|�r�tdk�r�t�A|�n
t�B|�|	�rt�|	�|�rt�C|�|�r(t�D|�|�r8t�E|�|�r�t�F|�nx|�rptdk�rft�A|�n
t�B|�|	�r�t�|	�|�r�t�C|�|�r�t�D|�|�r�t�E|�|�r�t�F|�0dS)aI
    Verifies an RSA, DSA or ECDSA signature

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to verify the signature with

    :param signature:
        A byte string of the signature to verify

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :param rsa_pss_padding:
        If the certificate_or_public_key is an RSA key, this enables PSS padding

    :raises:
        oscrypto.errors.SignatureError - when the signature is determined to be invalid
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library
    r�zA
            signature must be a byte string, not %s
            r�rMrL��md5�sha1�sha224�sha256�sha384�sha512�raw�5"md5", "sha1", "sha224", "sha256", "sha384", "sha512"�, "raw"�B
            hash_algorithm must be one of %s, not %s
            �o
            PSS padding can only be used with RSA keys - the key provided is a
            %s key
            ���
                data must be 11 bytes shorter than the key size when
                hash_algorithm is "raw" - key size is %s bytes, but data is
                %s bytes long
                NrzSignature is invalid�r r rprdrk�EVP_PKEY_CTX **����rKrr )Gr�rr8r�rr(r*rNrgrurvr�rS�	byte_sizer!r�rBrr$r�rZRSA_public_decryptr"r�rr,r'rwr#�EVP_MD_CTX_create�EVP_MD_CTX_new�EVP_md5�EVP_sha1�
EVP_sha224�
EVP_sha256�
EVP_sha384�
EVP_sha512�getattr�hashlib�digest�RSA_NO_PADDINGZRSA_verify_PKCS1_PSS�EVP_MD_CTX_FLAG_PSS_MDLEN�EVP_DigestInit_exr�EVP_DigestUpdateZEVP_VerifyFinalrZd2i_DSA_SIG�EVP_PKEY_get1_DSAZ
DSA_do_verifyZ
d2i_ECDSA_SIG�EVP_PKEY_get1_EC_KEYZECDSA_do_verifyrZEVP_DigestVerifyInitr�EVP_PKEY_CTX_ctrl�EVP_PKEY_RSA�EVP_PKEY_CTRL_RSA_PADDING�RSA_PKCS1_PSS_PADDING�EVP_PKEY_OP_SIGN�EVP_PKEY_OP_VERIFY�EVP_PKEY_CTRL_RSA_PSS_SALTLENZEVP_DigestVerifyFinal�EVP_MD_CTX_destroy�EVP_MD_CTX_freerx�DSA_SIG_freery�ECDSA_SIG_free)r�r�r�r�r�r�Z	cp_is_rsa�valid_hash_algorithms�valid_hash_algorithms_errorrMrUZdecrypted_bufferZdecrypted_lengthZdecrypted_bytes�
evp_md_ctxrd�dsa_sigr�	ecdsa_sig�evp_mdr�Zdecoded_bufferZdecoded_lengthr��signature_buffer�signature_pointer�evp_pkey_ctx_pointer_pointer�evp_pkey_ctx_pointerrGrGrHr��s��
�
����


�

��

��	


���






��

�







�





r�cCs*|jdkrttd|j�����t|||�S)a^
    Generates an RSASSA-PKCS-v1.5 signature.

    When the hash_algorithm is "raw", the operation is identical to RSA
    private key encryption. That is: the data is not hashed and no ASN.1
    structure with an algorithm identifier of the hash algorithm is placed in
    the encrypted byte string.

    :param private_key:
        The PrivateKey to generate the signature with

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384",
        "sha512" or "raw"

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the signature
    rM�M
            The key specified is not an RSA private key, but %s
            �rNrurr��_sign�r�r�r�rGrGrHr=�s
�r=cCs8|j}|dkr(|dkr(ttd|�����t|||dd�S)a.
    Generates an RSASSA-PSS signature. For the PSS padding the mask gen
    algorithm will be mgf1 using the same hash algorithm as the signature. The
    salt length with be the length of the hash algorithm, and the trailer field
    with be the standard 0xBC byte.

    :param private_key:
        The PrivateKey to generate the signature with

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the signature
    rMrLr�Tr�r�)r�r�r��pkey_algrGrGrHr?	s�r?cCs*|jdkrttd|j�����t|||�S)aA
    Generates a DSA signature

    :param private_key:
        The PrivateKey to generate the signature with

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the signature
    rdzL
            The key specified is not a DSA private key, but %s
            r�r�rGrGrHr-/s
�r-cCs*|jdkrttd|j�����t|||�S)aD
    Generates an ECDSA signature

    :param private_key:
        The PrivateKey to generate the signature with

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the signature
    rkzL
            The key specified is not an EC private key, but %s
            r�r�rGrGrHr/Ps
�r/c	CsPt|t�sttdt|����t|t�s8ttdt|����|j}|dkpL|dk}tgd��}|dkrt|st|tdg�O}||vr�d}|r�|s�|d7}ttd	|t	|����|s�|r�ttd
|�
����|�r||dk�r|t|�|jdkr�ttd|jt|����d
}znt
�|j�}t|��rtd�t|j�}	t|	�}
t
�t|�||
|tj�}t|�t|
|�W|�rft
�|�Sn|�rzt
�|�0d
}d
}d
}
d
}d
}d
}�z<tdk�r�t
��}nt
��}t
jt
jt
jt
jt
j t
j!d�|�}tdk�r|�rt|�rtt"t#|�|��$�}t
�|j�}t|��rtd�t|j�}	t|	�}t
�%||||tj&�}t|�t|	�}
t
�|	||
|tj'�}t|��q�|�r�t|j�}	t|	�}
t(t
d�}t
�)||t*��}t|�t
�+||t|��}t|�t
�,||
||j�}t|�t-|�}�q�|dk�r�t"t#|�|��$�}t
�.|j�}
t|
��r(td�t
�/|t|�|
�}t|��rLtd�t
�0|t*��}	t|	�}
t1|
�}t
�0||�}t|�n�|dk�r�t"t#|�|��$�}t
�2|j�}t|��r�td�t
�3|t|�|�}t|��r�td�t
�4|t*��}	t|	�}
t1|
�}t
�4||�}t|�n�t|j�}	t|	�}
t(t
d|	�}t(t
d�}t
�5|||t*�|j�}t|�t6|�}|�r�t
�7|tj8dtj9tj:t*��}t|�tdk�r�t
�7|tj8tj;tj<Btj=dt*��}t|�t
�+||t|��}t|�t
�>||
|�}t|�t-|�}t|
|�W|�r&tdk�rt
�?|�n
t
�@|�|�r6t
�|�|
�rFt
�A|
�|�rVt
�B|�|�rft
�C|�|�rvt
�D|�St
�@|�|�r�t
�|�|
�r�t
�A|
�|�r�t
�B|�|�r�t
�C|�|�rLt
�D|�nx|�r�tdk�r�t
�?|�n
t
�@|�|�r
t
�|�|
�rt
�A|
�|�r*t
�B|�|�r:t
�C|�|�rJt
�D|�0d
S)a�
    Generates an RSA, DSA or ECDSA signature

    :param private_key:
        The PrivateKey to generate the signature with

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha224", "sha256", "sha384" or "sha512"

    :param rsa_pss_padding:
        If the private_key is an RSA key, this enables PSS padding

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the signature
    zO
            private_key must be an instance of PrivateKey, not %s
            r�rMrLr�r�r�r�r�r�r�r�Nrr�rpzunsigned int *rdrkzsize_t *r�r�r�)Er�r7r�rr(r*rNrgrurvr�rSr�r!r�rBrr$r�rZRSA_private_encryptr"r�rrwr#r�r�r�r�r�r�r�r�r�r�r�ZRSA_padding_add_PKCS1_PSSr�r�rr�rr�Z
EVP_SignFinalrr�ZDSA_do_signZi2d_DSA_SIGrr�Z
ECDSA_do_signZ
i2d_ECDSA_SIGZEVP_DigestSignInitrr�r�r�r�r�r�r�ZEVP_DigestSignFinalr�r�rxr�ryr�)r�r�r�r�r�Zpkey_is_rsar�r�rMrUr�Zsignature_lengthr�rdr�rr�r�r�Z	em_bufferr�r�r�r�rGrGrHr�qs�
�
����


�

��

��	


��

�









��

�







�




�





r�)NN)N)N)N)F)F)Q�
__future__rrrrr�Z_asn1rr�rr	r
rrZ_asymmetricr
rrrrrrrr�_errorsrZ_ffirrrrrrrrrZ
_libcryptor!r"r#r$�errorsr%r&r'�_typesr(r)r*r+�utilr,�__all__r7r8r1r�r2r�r4r5r�r6r3r<r;r:r9r�r�r�r>r@r.r0r�r=r?r-r/r�rGrGrGrH�<module>sT ,,Tl
OC'
8F

=87('""
'&!!