o
    Œõ±j;ö  ã                   @   s  d dl mZmZmZmZ d dlZddlmZm	Z	m
Z
mZmZmZ ddlmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZmZmZmZmZm Z m!Z!m"Z"m#Z# dd	l$m%Z%m&Z&m'Z'm(Z( dd
l)m*Z*m+Z+m,Z, ddl-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3 g d¢Z4G dd„ deƒZ5G dd„ deƒZ6G dd„ deƒZdIdd„Z7dd„ Z8dd„ Z9dd„ Z:dJdd„Z;dd„ Z<d d!„ Z=dJd"d#„Z>dJd$d%„Z?d&d'„ Z@d(d)„ ZAd*d+„ ZBd,d-„ ZCd.d/„ ZDd0d1„ ZEd2d3„ ZFd4d5„ ZGd6d7„ ZHd8d9„ ZId:d;„ ZJdKd=d>„ZKd?d@„ ZLdAdB„ ZMdCdD„ ZNdEdF„ ZOdKdGdH„ZPdS )Lé    )Ú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                   @   sD   e Zd ZdZdZdZdZdd„ Zedd„ ƒZ	edd„ ƒZ
d	d
„ ZdS )r7   zC
    Container for the OpenSSL representation of a private key
    Nc                 C   ó   || _ || _t| _dS )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©ÚselfrC   rD   © rH   ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/oscrypto/_openssl/asymmetric.pyÚ__init__P   ó   	
zPrivateKey.__init__c           
      C   sÊ   | j du rbt | jtƒ ¡}t|ƒ}t|ƒ}t | j|¡}t|ƒ t||ƒ}t	 
|¡}tdk rJ|jdkrJ| ¡ }d|d d< | ¡ }t||ƒ t|ƒ}t tƒ t|ƒ|¡}	t|	ƒr\tdƒ t|	|ƒ| _ | j S )z\
        :return:
            A PublicKey object corresponding to this private key.
        N©é   Ú
rsassa_pssÚrsaÚ	algorithmr   )Ú_public_keyr!   Ú
i2d_PUBKEYrC   r   r   r   r$   r   r   Úloadr#   rP   ÚcopyÚdumpr   ÚlenÚ
d2i_PUBKEYr   r8   )
rG   Úbuffer_sizeÚpubkey_bufferÚpubkey_pointerÚpubkey_lengthÚpubkey_datarD   Ú	temp_asn1Ú	temp_dataÚpub_evp_pkeyrH   rH   rI   Ú
public_key]   s&   



zPrivateKey.public_keyc                 C   s   | j du rt | jtƒ| _ | j S )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)r   rD   r4   ©rG   rH   rH   rI   Úfingerprint   s   
zPrivateKey.fingerprintc                 C   ó(   | j r| j | j ¡ d | _d | _ d S d S ©N©rC   rE   ÚEVP_PKEY_freera   rH   rH   rI   Ú__del__‘   ó
   
ýzPrivateKey.__del__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rC   rQ   rE   rJ   Úpropertyr`   rb   rg   rH   rH   rH   rI   r7   D   s    
!
r7   c                   @   s(   e Zd ZdZdZdZdd„ Zdd„ ZdS )r8   zB
    Container for the OpenSSL representation of a public key
    Nc                 C   rA   )z±
        :param evp_pkey:
            An OpenSSL EVP_PKEY value from loading/importing the key

        :param asn1:
            An asn1crypto.keys.PublicKeyInfo object
        NrB   rF   rH   rH   rI   rJ   £   rK   zPublicKey.__init__c                 C   rc   rd   re   ra   rH   rH   rI   rg   °   rh   zPublicKey.__del__)ri   rj   rk   rl   rC   rE   rJ   rg   rH   rH   rH   rI   r8   ˜   s    r8   c                   @   sT   e Zd ZdZdZdZdZdZdd„ Ze	dd„ ƒZ
e	dd„ ƒZe	d	d
„ ƒZdd„ ZdS )r   zC
    Container for the OpenSSL representation of a certificate
    Nc                 C   rA   )z¯
        :param x509:
            An OpenSSL X509 value from loading/importing the certificate

        :param asn1:
            An asn1crypto.x509.Certificate object
        N)Úx509rD   r!   rE   )rG   rn   rD   rH   rH   rI   rJ   Ä   rK   zCertificate.__init__c                 C   s   | j jS )z_
        :return:
            The EVP_PKEY of the public key this certificate contains
        )r`   rC   ra   rH   rH   rI   rC   Ñ   s   zCertificate.evp_pkeyc                 C   sX   | j s)| jr)tdk r| jjjdkrt| jjƒ| _ | j S t | j¡}t	|| jjƒ| _ | j S )zh
        :return:
            The PublicKey object for the public key this certificate contains
        rL   rN   )
rQ   rn   r#   rD   r`   rP   r5   r!   ÚX509_get_pubkeyr8   )rG   rC   rH   rH   rI   r`   Ú   s   ýzCertificate.public_keyc                 C   sÐ   | j du red| _ | jjtddgƒv re| jd j}| jd j}|dkr%t}n|dkr,t}n|dkr3t}n|d	kr:t	}nt
td
|ƒƒ‚z|| j| jd j| jd  ¡ |ƒ d| _ W | j S  tyd   Y | j S w | j S )zT
        :return:
            A boolean - if the certificate is self-signed
        NFÚyesÚmaybeÚsignature_algorithmÚrsassa_pkcs1v15rN   ÚdsaÚecdsaz¦
                        Unable to verify the signature of the certificate since
                        it uses the unsupported algorithm %s
                        Úsignature_valueÚtbs_certificateT)Ú_self_signedrD   Úself_signedÚsetÚsignature_algoÚ	hash_algor>   r@   r.   r0   ÚOSErrorr   r`   ÚnativerU   r'   )rG   r{   r|   Úverify_funcrH   rH   rI   ry   ì   s>   
û
üýýzCertificate.self_signedc                 C   s>   | j r| j  ¡  d | _ | jr| j | j¡ d | _d | _d S d S rd   )rQ   rg   rn   rE   Ú	X509_freera   rH   rH   rI   rg     s   

ýzCertificate.__del__)ri   rj   rk   rl   rn   rQ   rx   rE   rJ   rm   rC   r`   ry   rg   rH   rH   rH   rI   r   ·   s    


+r   c              	   C   s¶  | t g d¢ƒvrttdt| ƒƒƒ‚| dkr'|t g d¢ƒvr&ttdt|ƒƒƒ‚n=| dkrOtdk r=|dkr<ttd	t|ƒƒƒ‚n'|t g d
¢ƒvrNttdt|ƒƒƒ‚n| dkrd|t g d¢ƒvrdttdt|ƒƒƒ‚| dk�rd}d}z�t ¡ }t|ƒrztdƒ t	tdƒ}t 
|d¡}t|ƒ t|ƒ}t |||tƒ ¡}t|ƒ t |tƒ ¡}|dk r©t|ƒ t|ƒ}t |t|ƒ¡}|dk r½t|ƒ t||ƒ}	t |tƒ ¡}|dk rÑt|ƒ t|ƒ}t |t|ƒ¡}|dk råt|ƒ t||ƒ}
W |ròt |¡ |rùt |¡ �nX|�rt |¡ |�rt |¡ w w | dk�r¦d}z‡t ¡ }t|ƒ�r"tdƒ t ||tƒ dtƒ tƒ tƒ ¡}t|ƒ t |¡}t|ƒ t |tƒ ¡}|dk �rNt|ƒ t|ƒ}t |t|ƒ¡}|dk �rct|ƒ t||ƒ}	t |tƒ ¡}|dk �rxt|ƒ t|ƒ}t |t|ƒ¡}|dk �r�t|ƒ t||ƒ}
W |�r›t |¡ n·|�r¥t |¡ w w | dk�rSd}z›tjtjtjdœ| }t |¡}t|ƒ�rÇtdƒ t  |¡}t|ƒ t !|tj"¡ t #|tƒ ¡}|dk �rçt|ƒ t|ƒ}t #|t|ƒ¡}|dk �rüt|ƒ t||ƒ}t$t%dt&d|d�dœƒ|dœƒ}| '¡ }	t (|tƒ ¡}|dk �r%t|ƒ t|ƒ}t (|t|ƒ¡}|dk �r:t|ƒ t||ƒ}
W |�rHt )|¡ n
|�rRt )|¡ w w t*|	ƒt+|
ƒfS )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().
    )rO   rt   ÚeczM
            algorithm must be one of "rsa", "dsa", "ec", not %s
            rO   )é   é   é   é   zX
                bit_size must be one of 1024, 2048, 3072, 4096, not %s
                rt   ©r    r‚   zG
                    bit_size must be 1024, not %s
                    )r‚   rƒ   r„   zZ
                    bit_size must be one of 1024, 2048, 3072, not %s
                    r�   )Ú	secp256r1Ú	secp384r1Ú	secp521r1zt
                curve must be one of "secp256r1", "secp384r1", "secp521r1",
                not %s
                Nr   z	BIGNUM **s   65537Únamed)ÚnameÚvalue)rP   Ú
parameters)rP   r`   ),rz   Ú
ValueErrorr   Úreprr#   r!   ÚRSA_newr   r$   r   Ú	BN_dec2bnr   ÚRSA_generate_key_exr   Úi2d_RSAPublicKeyr   r   r   Úi2d_RSAPrivateKeyÚRSA_freeÚBN_freeÚDSA_newÚDSA_generate_parameters_exÚDSA_generate_keyÚi2d_DSA_PUBKEYÚi2d_DSAPrivateKeyÚDSA_freer"   ÚNID_X9_62_prime256v1ÚNID_secp384r1ÚNID_secp521r1ÚEC_KEY_new_by_curve_nameÚEC_KEY_generate_keyÚEC_KEY_set_asn1_flagÚOPENSSL_EC_NAMED_CURVEÚi2o_ECPublicKeyr   r   r	   rU   Úi2d_ECPrivateKeyÚEC_KEY_freer5   r4   )rP   Úbit_sizeÚcurverO   ÚexponentÚexponent_pointerÚresultÚbuffer_lengthÚbufferÚpublic_key_bytesÚprivate_key_bytesrt   Úec_keyÚcurve_idÚpublic_key_point_bytesr`   rH   rH   rI   r1   #  s0  üüÿüÿüÿû




€ý
ÿ








€ÿÿ
ýü





þþø



€ÿÿr1   c              	   C   s  t | tƒsttdt| ƒƒƒ‚| dk rtdƒ‚| dkrtdƒ‚| d dkr(tdƒ‚d	}zPt ¡ }t|ƒr7t	dƒ t 
|| tjtƒ ¡}t	|ƒ t |tƒ ¡}|dk rTt	|ƒ t|ƒ}t |t|ƒ¡}|dk rht	|ƒ t||ƒ}t |¡W |rzt |¡ S S |rƒt |¡ w w )
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
            i   z-bit_size must be greater than or equal to 512r…   z+bit_size must be less than or equal to 4096é@   r   z!bit_size must be a multiple of 64N)Ú
isinstancer+   Ú	TypeErrorr   r(   rŽ   r!   ÚDH_newr   r$   ÚDH_generate_parameters_exr"   ÚDH_GENERATOR_2r   Úi2d_DHparamsr   r   r   r   rS   ÚDH_free)r§   Údhr«   r¬   r­   Údh_params_bytesrH   rH   rI   Úgenerate_dh_parametersñ  s@   
ü

ÿÿr½   c                 C   s�   t | tƒr| }t
|ƒS t | tƒrt| ƒ}t
|ƒS t | tƒr?t| dƒ�}t| ¡ ƒ}W d  ƒ t
|ƒS 1 s6w   Y  t
|ƒS ttdt	| ƒƒƒ‚)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*   r   r)   ÚopenÚreadrµ   r   r(   Ú
_load_x509)ÚsourceÚcertificateÚfrH   rH   rI   r2   4  s"   

ð
ó
ÿôøûr2   c                 C   sB   |   ¡ }t|ƒ}t tƒ t|ƒt|ƒ¡}t|ƒrtdƒ 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   )
rU   r   r!   Úd2i_X509r   r   rV   r   r$   r   )rÄ   rÃ   r­   rC   rH   rH   rI   rÂ   [  s   
rÂ   c                 C   sº   t | tƒr| }t|ƒS |dur't |tƒr| d¡}t |tƒs'ttdt|ƒƒƒ‚t | tƒrFt| dƒ�}| 	¡ } W d  ƒ n1 s@w   Y  nt | tƒsTttdt| ƒƒƒ‚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µ   r   r(   rÀ   rÁ   r   Ú	_load_key)rÃ   ÚpasswordÚprivate_objectrÅ   rH   rH   rI   r4   o  s.   
æ


ü

ÿ€
û
r4   c                 C   s,  t | tƒr| }n4t | tƒrt| ƒ}n*t | tƒr3t| dƒ�}t| ¡ ƒ}W d  ƒ n1 s-w   Y  n	ttdt	| ƒƒƒ‚|j
dkr]tdk rR|jdkrRttd|jƒƒ‚|jdu r]ttdƒƒ‚td	k ru|j
d
kru| ¡ }d|d d< | ¡ }n| ¡ }t|ƒ}t tƒ t|ƒt|ƒ¡}t|ƒr‘tdƒ 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
            rt   r†   Ú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
                rL   rN   rO   rP   r   )r´   r   r*   r   r)   rÀ   rÁ   rµ   r   r(   rP   r#   r|   r%   r§   r&   rT   rU   r   r!   rW   r   r   rV   r   r$   r8   )rÃ   r`   rÅ   Útemp_keyÚdatar­   rC   rH   rH   rI   r5   §  sB   



ÿ€û
û
ÿ	

r5   c                 C   sr   t dk r| jdkr| jdkrttd| jƒƒ‚t| ƒ ¡ }t|ƒ}t	 
tƒ t|ƒt|ƒ¡}t|ƒr4tdƒ t|| ƒS )z­
    Loads a private key into a PrivateKey object

    :param private_object:
        An asn1crypto.keys.PrivateKeyInfo object

    :return:
        A PrivateKey object
    r†   rt   rÌ   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#   rP   r|   r%   r   r§   r   rU   r   r!   Úd2i_AutoPrivateKeyr   r   rV   r   r$   r7   )rË   rÃ   r­   rC   rH   rH   rI   rÉ   í  s   û
rÉ   c                 C   s   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
    )r   r4   )rÎ   rÊ   rH   rH   rI   r6   
  s   r6   c           	      C   sÚ   |durt |tƒr| d¡}t |tƒsttdt|ƒƒƒ‚t | tƒr;t| dƒ�}| ¡ } W d  ƒ n1 s5w   Y  nt | tƒsIttdt| ƒƒƒ‚t	| |ƒ\}}}d}d}|r[t
|ƒ}|rat|ƒ}dd„ |D ƒ}|||fS )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
            c                 S   s   g | ]}t |ƒ‘qS rH   )rÂ   )Ú.0ÚinforH   rH   rI   Ú
<listcomp>[  s    zload_pkcs12.<locals>.<listcomp>)r´   r)   rÈ   r*   rµ   r   r(   rÀ   rÁ   r6   rÉ   rÂ   )	rÃ   rÊ   rÅ   Úkey_infoÚ	cert_infoÚextra_certs_infoÚkeyÚcertÚextra_certsrH   rH   rI   r3   #  s6   


ü

ÿ€
ü
r3   c                 C   ó   t | |tjƒ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Î   rH   rH   rI   r<   `  ó   r<   c                 C   rÙ   )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Ú
ciphertextrH   rH   rI   r;   x  s   r;   c                 C   rÙ   )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Ü   rH   rH   rI   r:   Ž  rÞ   r:   c                 C   rÙ   )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à   rH   rH   rI   r9   ¦  s   r9   c                 C   s   t dk r	t | ¡S t | ¡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
    rL   )r#   r!   ÚEVP_PKEY_sizeÚEVP_PKEY_get_size)rC   rH   rH   rI   Ú_evp_pkey_get_size½  s   

ræ   c              	   C   s¬   t | ttfƒsttdt| ƒƒƒ‚t |tƒsttdt|ƒƒƒ‚d}z,t| jƒ}t	|ƒ}t
 | j¡}t
 t|ƒ||||¡}t|ƒ t||ƒW |rLt
 |¡ S S |rUt
 |¡ w w )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´   r   r8   rµ   r   r(   r*   ræ   rC   r   r!   ÚEVP_PKEY_get1_RSAÚRSA_public_encryptrV   r$   r   r•   )rÝ   rÎ   ÚpaddingrO   rX   r­   ÚresrH   rH   rI   rÚ   Í  s.   û
ü

ÿÿrÚ   c              	   C   s¨   t | tƒsttdt| ƒƒƒ‚t |tƒsttdt|ƒƒƒ‚d}z,t| jƒ}t|ƒ}t	 
| j¡}t	 t|ƒ||||¡}t|ƒ t||ƒW |rJt	 |¡ S S |rSt	 |¡ w w )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´   r7   rµ   r   r(   r*   ræ   rC   r   r!   ré   ÚRSA_private_decryptrV   r$   r   r•   )rá   râ   rë   rO   rX   r­   rì   rH   rH   rI   rß     s.   
ü
ü

ÿÿrß   c                 C   ó,   | j dkrttd| 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
    rO   úL
            The key specified is not an RSA public key, but %s
            ©rP   rŽ   r   ÚupperÚ_verify©rÝ   Ú	signaturerÎ   Úhash_algorithmrH   rH   rI   r>   <  s   
ür>   c                 C   s<   | j }|dkr|dkrttd| j  ¡ ƒƒ‚t| |||dd�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
    rO   rN   rï   T©Úrsa_pss_paddingrð   )rÝ   rô   rÎ   rõ   Úcp_algrH   rH   rI   r@   d  s   ür@   c                 C   rî   )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
    rt   zK
            The key specified is not a DSA public key, but %s
            rð   ró   rH   rH   rI   r.   ‹  ó   
ür.   c                 C   rî   )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
    r�   zK
            The key specified is not an EC public key, but %s
            rð   ró   rH   rH   rI   r0   ­  rù   r0   Fc              	   C   s¼  t | ttfƒsttdt| ƒƒƒ‚t |tƒsttdt|ƒƒƒ‚t |tƒs,ttdt|ƒƒƒ‚| j}|dkp6|dk}tg d¢ƒ}|rH|sH|tdgƒO }||vr`d}|rV|sV|d	7 }t	td
|t
|ƒƒƒ‚|sm|rmt	td| ¡ ƒƒ‚|rÔ|dkrÔt|ƒ| jd kr‡t	td| jt|ƒƒƒ‚d}	zAt | j¡}	t|	ƒr˜tdƒ t| jƒ}
t|
ƒ}t t|ƒ|||	tj¡}t|ƒ t||ƒ}t||ƒs¿tdƒ‚W |	rÉt |	¡ dS dS |	rÓt |	¡ w w d}d}	d}d}d}d}�z¿tdk rët ¡ }nt ¡ }tjtj tj!tj"tj#tj$dœ| ƒ }tdk �r÷|�rI|�rIt%t&|ƒ|ƒ '¡ }t | j¡}	t|	ƒ�r$tdƒ t| jƒ}
t|
ƒ}t t|ƒ|||	tj(¡}t|ƒ t )|	|||tj*¡}�n|�rqt +||t,ƒ ¡}t|ƒ t -||t|ƒ¡}t|ƒ t .||t|ƒ| j¡}nã|dk�r´t%t&|ƒ|ƒ '¡ }t|ƒ}t/|ƒ}t 0t,ƒ |t|ƒ¡}t|ƒ�rštdƒ‚t 1| j¡}t|ƒ�r©tdƒ t 2|t|ƒ||¡}n |dk�röt%t&|ƒ|ƒ '¡ }t|ƒ}t/|ƒ}t 3t,ƒ |t|ƒ¡}t|ƒ�rÝtdƒ‚t 4| j¡}t|ƒ�rìtdƒ t 5|t|ƒ||¡}n]t6tdƒ}t 7|||t,ƒ | j¡}t|ƒ t8|ƒ}|�r>t 9|tj:dtj;tj<t,ƒ ¡}t|ƒ tdk �r>t 9|tj:tj=tj>B tj?dt,ƒ ¡}t|ƒ t -||t|ƒ¡}t|ƒ t @||t|ƒ¡}|dk �r]tdƒ‚t|ƒ W |�rutdk �rpt A|¡ nt B|¡ |	�r}t |	¡ |�r…t C|¡ |�r�t D|¡ |�r•t E|¡ |�rŸt F|¡ dS dS |�r´tdk �r¯t A|¡ nt B|¡ |	�r¼t |	¡ |�rÄt C|¡ |�rÌt D|¡ |�rÔt E|¡ |�rÝt F|¡ w w )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è   rO   rN   ©Ú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
                Nr   zSignature is invalid©r    r    r†   rt   r�   úEVP_PKEY_CTX **éÿÿÿÿ©rM   r   r    )Gr´   r   r8   rµ   r   r(   r*   rP   rz   rŽ   r�   rñ   rV   Ú	byte_sizer!   ré   rC   r   r$   ræ   r   ÚRSA_public_decryptr"   rÛ   r   r,   r'   r•   r#   Ú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_PADDINGÚRSA_verify_PKCS1_PSSÚEVP_MD_CTX_FLAG_PSS_MDLENÚEVP_DigestInit_exr   ÚEVP_DigestUpdateÚEVP_VerifyFinalr   Úd2i_DSA_SIGÚEVP_PKEY_get1_DSAÚDSA_do_verifyÚd2i_ECDSA_SIGÚEVP_PKEY_get1_EC_KEYÚECDSA_do_verifyr   ÚEVP_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_SALTLENÚEVP_DigestVerifyFinalÚEVP_MD_CTX_destroyÚEVP_MD_CTX_freerœ   ÚDSA_SIG_freer¦   ÚECDSA_SIG_free)rÝ   rô   rÎ   rõ   r÷   rø   Ú	cp_is_rsaÚvalid_hash_algorithmsÚvalid_hash_algorithms_errorrO   rX   Údecrypted_bufferÚdecrypted_lengthÚdecrypted_bytesÚ
evp_md_ctxrt   Údsa_sigr°   Ú	ecdsa_sigÚevp_mdr  Údecoded_bufferÚdecoded_lengthrì   Úsignature_bufferÚsignature_pointerÚevp_pkey_ctx_pointer_pointerÚevp_pkey_ctx_pointerrH   rH   rI   rò   Ï  sž  û
ü
üûûù

û

ÿÿ
úù
	

ûûü





€
ûú

ú







ÿó





ÿrò   c                 C   ó*   | j dkrttd| 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
    rO   úM
            The key specified is not an RSA private key, but %s
            ©rP   rŽ   r   rñ   Ú_sign©rá   rÎ   rõ   rH   rH   rI   r=   â  s   
ür=   c                 C   s8   | j }|dkr|dkrttd| ¡ ƒƒ‚t| ||dd�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
    rO   rN   rC  Trö   rD  )rá   rÎ   rõ   Úpkey_algrH   rH   rI   r?   	  s   ür?   c                 C   rB  )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
    rt   zL
            The key specified is not a DSA private key, but %s
            rD  rF  rH   rH   rI   r-   /  ó   
ür-   c                 C   rB  )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
    r�   zL
            The key specified is not an EC private key, but %s
            rD  rF  rH   rH   rI   r/   P  rH  r/   c              	   C   sê  t | tƒsttdt| ƒƒƒ‚t |tƒsttdt|ƒƒƒ‚| j}|dkp&|dk}tg d¢ƒ}|dkr:|s:|tdgƒO }||vrRd}|rH|sH|d7 }ttd	|t	|ƒƒƒ‚|s_|r_ttd
| 
¡ ƒƒ‚|rº|dkrºt|ƒ| jd kryttd| jt|ƒƒƒ‚d}z5t | j¡}t|ƒrŠtdƒ t| jƒ}	t|	ƒ}
t t|ƒ||
|tj¡}t|ƒ t|
|ƒW |r°t |¡ S S |r¹t |¡ w w d}d}d}d}d}d}�zðtdk rÑt ¡ }nt ¡ }tjtjtjtjtj tj!dœ| ƒ }tdk �r|�r5|�r5t"t#|ƒ|ƒ $¡ }t | j¡}t|ƒ�r
tdƒ t| jƒ}	t|	ƒ}t %||||tj&¡}t|ƒ t|	ƒ}
t |	||
|tj'¡}t|ƒ �nA|�rrt| jƒ}	t|	ƒ}
t(tdƒ}t )||t*ƒ ¡}t|ƒ t +||t|ƒ¡}t|ƒ t ,||
|| j¡}t|ƒ t-|ƒ}�n|dk�r»t"t#|ƒ|ƒ $¡ }t .| j¡}t|ƒ�r�tdƒ t /|t|ƒ|¡}t|ƒ�r¡tdƒ t 0|t*ƒ ¡}	t|	ƒ}
t1|
ƒ}t 0||¡}t|ƒ n»|dk�rt"t#|ƒ|ƒ $¡ }t 2| j¡}t|ƒ�rØtdƒ t 3|t|ƒ|¡}t|ƒ�rêtdƒ t 4|t*ƒ ¡}	t|	ƒ}
t1|
ƒ}t 4||¡}t|ƒ nrt| jƒ}	t|	ƒ}
t(td|	ƒ}t(tdƒ}t 5|||t*ƒ | j¡}t|ƒ t6|ƒ}|�rZt 7|tj8dtj9tj:t*ƒ ¡}t|ƒ tdk �rZt 7|tj8tj;tj<B tj=dt*ƒ ¡}t|ƒ t +||t|ƒ¡}t|ƒ t >||
|¡}t|ƒ t-|ƒ}t|
|ƒW |�rŽtdk �r‰t ?|¡ nt @|¡ |�r–t |¡ |�ržt A|¡ |�r¦t B|¡ |�r®t C|¡ |�r·t D|¡ S S |�rËtdk �rÆt ?|¡ nt @|¡ |�rÓt |¡ |�rÛt A|¡ |�rãt B|¡ |�rët C|¡ |�rôt D|¡ w w )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è   rO   rN   rú   r  r  r  r  r  r  r  Nr   r  r†   zunsigned int *rt   r�   zsize_t *r	  r
  r  )Er´   r7   rµ   r   r(   r*   rP   rz   rŽ   r�   rñ   rV   r  r!   ré   rC   r   r$   ræ   r   ÚRSA_private_encryptr"   rÛ   r   r•   r#   r  r  r  r  r  r  r  r  r  r  r  ÚRSA_padding_add_PKCS1_PSSr  r  r   r  r   r  ÚEVP_SignFinalr   r   ÚDSA_do_signÚi2d_DSA_SIGr   r#  ÚECDSA_do_signÚi2d_ECDSA_SIGÚEVP_DigestSignInitr   r&  r'  r(  r)  r*  r+  r,  ÚEVP_DigestSignFinalr.  r/  rœ   r0  r¦   r1  )rá   rÎ   rõ   r÷   rG  Úpkey_is_rsar3  r4  rO   rX   r>  Úsignature_lengthr8  rt   r9  r°   r:  r;  r  Ú	em_bufferrì   r?  r@  rA  rH   rH   rI   rE  q  sª  
ü
üûûù

û
ÿÿ
úù
	

ûû

ü






€

ûú

ú






ÿó





ÿrE  )NNrd   )F)QÚ
__future__r   r   r   r   r  Ú_asn1r   r¿   r   r	   r
   r   r   Ú_asymmetricr   r   r   r   r   r   r   r   r   Ú_errorsr   Ú_ffir   r   r   r   r   r   r   r   r   Ú
_libcryptor!   r"   r#   r$   Úerrorsr%   r&   r'   Ú_typesr(   r)   r*   r+   Úutilr,   Ú__all__r7   r8   r1   r½   r2   rÂ   r4   r5   rÉ   r6   r3   r<   r;   r:   r9   ræ   rÚ   rß   r>   r@   r.   r0   rò   r=   r?   r-   r/   rE  rH   rH   rH   rI   Ú<module>   sV    ,,T
l OC'
8F

=87('"
"  '&!!