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: //var/opt/nydus/ops/oscrypto/_mac/__pycache__/asymmetric.cpython-39.pyc
a

�,�h��@sLddlmZmZmZmZddlmZddlZddlZddl	Z	ddl
mZm
Z
mZmZmZmZmZmZmZddlmZmZmZmZmZmZmZmZmZddlm Z ddl!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	dl/m0Z0m1Z1m2Z2d	dl3m4Z4dd
l5m6Z6m7Z7m8Z8ddl9m:Z:m;Z;m<Z<ddl=m>Z>m?Z?m@Z@mAZAgd�ZBGdd�de�ZCGdd�de�ZDGdd�de�ZdIdd�ZEdd�ZFdd�ZGdd�ZHdJdd �ZId!d"�ZJd#d$�ZKdKd%d&�ZLdLd'd(�ZMd)d*�ZNd+d,�ZOd-d.�ZPd/d0�ZQd1d2�ZRd3d4�ZSd5d6�ZTd7d8�ZUd9d:�ZVd;d<�ZWd=d>�ZXd?d@�ZYdAdB�ZZdCdD�Z[dEdF�Z\dGdH�Z]dS)M�)�unicode_literals�division�absolute_import�print_function)�	b32encodeN�)	�Certificate�ECDomainParameters�Integer�KeyExchangeAlgorithm�Null�PrivateKeyInfo�PublicKeyAlgorithm�
PublicKeyInfo�RSAPublicKey)	�_CertificateBase�_fingerprint�
_parse_pkcs12�_PrivateKeyBase�_PublicKeyBase�_unwrap_private_key_info�parse_certificate�
parse_private�parse_public)�pretty_message)�new�unwrap�bytes_from_buffer�buffer_from_bytes�deref�null�is_null�pointer_set�)�Security�
SecurityConst�handle_sec_error�osx_version_info)�CoreFoundation�	CFHelpers�handle_cf_error)�
rand_bytes)�AsymmetricKeyError�IncompleteAsymmetricKeyError�SignatureError)�add_pss_padding�verify_pss_padding�"remove_pkcs1v15_encryption_padding)�	type_name�str_cls�byte_cls�	int_types)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)r@zM
    Container for the OS crypto library representation of a private key
    NcCs||_||_t|_dS)z�
        :param sec_key_ref:
            A Security framework SecKeyRef value from loading/importing the
            key

        :param asn1:
            An asn1crypto.keys.PrivateKeyInfo object
        N��sec_key_ref�asn1r(�_lib��selfrKrL�rP�I/opt/nydus/tmp/pip-target-wkfpz8uv/lib/python/oscrypto/_mac/asymmetric.py�__init__Ms
zPrivateKey.__init__c	Cs\|jdu�rVd}�z&ttd�}t�|jddt�|�}t|�t|�}t	�
|�}t|�}|jdkr�t
tdt�d��t|djd|djdd	��d
��}n�|jdkr�|dd
}t
td|��d��tt|dj|djj|dj��d
��}n8|jdk�r&t
tdtd|jd�d��|djdd
��}W|�rLt�|�n|�rJt�|�0t|�|_|jS)z\
        :return:
            A PublicKey object corresponding to this private key.
        N�CFDataRef *r�rsa)�	algorithm�
parameters�private_key�modulus�public_exponent)rXrY)rU�
public_key�dsaZprivate_key_algorithmrV�g�p�ec�named)�name�valuerZ)�_public_keyrr(r$�
SecItemExportrKr r&rr)�cf_data_to_bytesrrUrrrr�parsed�copyr
�pow�nativer	�curve�	CFRelease�	_load_key)rO�cf_data_private�cf_data_private_pointer�result�private_key_bytes�keyZpublic_asn1�paramsrPrPrQrZ[sd


���

�
��
���
�
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)rrLr=�rOrPrPrQ�fingerprint�s
zPrivateKey.fingerprintcCs$|jr |j�|j�d|_d|_dS�N�rKrMrjrrrPrPrQ�__del__�szPrivateKey.__del__)�__name__�
__module__�__qualname__�__doc__rKrbrMrR�propertyrZrsrvrPrPrPrQr@As
A
r@c@s(eZdZdZdZdZdd�Zdd�ZdS)rAzL
    Container for the OS crypto library representation of a public key
    NcCs||_||_t|_dS)z�
        :param sec_key_ref:
            A Security framework SecKeyRef value from loading/importing the
            key

        :param asn1:
            An asn1crypto.keys.PublicKeyInfo object
        NrJrNrPrPrQrR�s
zPublicKey.__init__cCs$|jr |j�|j�d|_d|_dSrtrurrrPrPrQrv�szPublicKey.__del__)rwrxryrzrKrMrRrvrPrPrPrQrA�s
rAc@sPeZdZdZdZdZdZdd�Zedd��Z	edd��Z
ed	d
��Zdd�ZdS)
rzM
    Container for the OS crypto library representation of a certificate
    NcCs||_||_dS)z�
        :param sec_certificate_ref:
            A Security framework SecCertificateRef value from loading/importing
            the certificate

        :param asn1:
            An asn1crypto.x509.Certificate object
        N)�sec_certificate_refrL)rOr|rLrPrPrQrR�s
zCertificate.__init__cCs|jjS)zF
        :return:
            The SecKeyRef of the public key
        )rZrKrrrPrPrQrK�szCertificate.sec_key_refcCs�|js�|jr�|jjdkrF|j��}d|dddd<t|�}|j}n|j}ttd�}t�||�}t	|�t
|�}t||jdd�|_|jS)zh
        :return:
            The PublicKey object for the public key this certificate contains
        �
rsassa_pssrT�tbs_certificateZsubject_public_key_inforU�SecKeyRef *)rbr|rL�signature_algorf�
_load_x509rr$ZSecCertificateCopyPublicKeyr&rrA)rOrLZ	temp_certZsec_cert_refZsec_public_key_ref_pointer�resZsec_public_key_refrPrPrQrZ�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_pkcs1v15r}r[Zecdsaz�
                        Unable to verify the signature of the certificate since
                        it uses the unsupported algorithm %s
                        Zsignature_valuer~T)�_self_signedrL�self_signed�setr��	hash_algorGrIr7r9�OSErrorrrZrh�dumpr.)rOr�r�Zverify_funcrPrPrQr�s8
�
�
zCertificate.self_signedcCs2|jr|j��d|_|jr.t�|j�d|_dSrt)rbrvr|r(rjrrrPrPrQrv;s
zCertificate.__del__)
rwrxryrzr|rbr�rRr{rKrZr�rvrPrPrPrQr�s



+rc
Cs�|tgd��vr"ttdt|����|dkrN|tgd��vr�ttdt|����nT|dkrx|tdg�vr�ttdt|����n*|d	kr�|tgd
��vr�ttdt|����d}d}d}d}d}d}d}	d}
d}�zvtjtjtjd
�|}|d	kr�dddd
�|}
n|}
tt	d�}tt	d�}t
�d�}d}td|�}|dd�}t
|dd���d�}t��}tj�||��d�}tt	d�}t	�|||dt�|�}t|�t|�}
tt	d�}t	�|t�|�}t|�t|�}	t	�|
||
dtjtjtjBtjtjtjB|	||�}t|�t|�}t|�}ttd�}t	� |ddt�|�}t|�t|�}t
�!|�}ttd�}t	� |ddt�|�}t|�t|�}t
�!|�}t	�"|�}t|�t	�"|�}t|�W|�r�t�#|�|�r�t�#|�|�r�t�#|�|�r�t�#|�|�r�t�#|�|�rt�#|�|
�rt	�$|
�t�#|
�|�r.t%�&|�|	�r�t�#|	�n�|�rPt�#|�|�r`t�#|�|�rpt�#|�|�r�t�#|�|�r�t�#|�|�r�t�#|�|
�r�t	�$|
�t�#|
�|�r�t%�&|�|	�r�t�#|	�0t'|�t(|�fS)a
    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.

    :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().
    )rTr[r^zM
            algorithm must be one of "rsa", "dsa", "ec", not %s
            rT)�ii�zX
                bit_size must be one of 1024, 2048, 3072, 4096, not %s
                r[r�z?
                bit_size must be 1024, not %s
                r^�Z	secp256r1Z	secp384r1Z	secp521r1zd
                curve must be one of "secp256r1", "secp384r1", "secp521r1", not %s
                N)r[r^rT��i	r�Temporary oscrypto key��
�utf-8�SecKeychainRef *F�SecAccessRef *rrS))r��
ValueErrorr�reprr%ZCSSM_ALGID_DSAZCSSM_ALGID_ECDSAZCSSM_ALGID_RSArr$r)�cf_string_from_unicoder+r�decode�tempfile�mkdtemp�os�path�join�encode�SecKeychainCreater r&r�SecAccessCreate�SecKeyCreatePairZCSSM_KEYUSE_VERIFY�CSSM_KEYATTR_EXTRACTABLE�CSSM_KEYATTR_PERMANENTZCSSM_KEYUSE_SIGNr(rcrd�SecKeychainItemDeleterj�SecKeychainDelete�shutil�rmtreer>r=)rU�bit_sizeriZcf_dict�public_key_ref�private_key_ref�cf_data_publicrl�	cf_string�sec_access_ref�sec_keychain_ref�temp_dirZalg_idZkey_size�private_key_pointer�public_key_pointer�passphrase_len�	rand_data�
passphrase�
temp_filename�	temp_path�sec_keychain_ref_pointerrn�sec_access_ref_pointerZcf_data_public_pointerZpublic_key_bytesrmrorPrPrQr:Es��������



�


�
















�








r:c
Cs�t|t�sttdt|����|dkr,td��|dkr<td��|ddkrPtd��d	}d	}d	}d	}d	}d	}d	}d	}�z�ttd
�}	ttd
�}
t�	d�}d}t
d
|�}|d
d	�}
t|d	d
���d�}t
��}tj�||��d�}ttd�}t�|||
dt�|�}t|�t|�}ttd�}t�|t�|�}t|�t|�}t�|tj|ddtjtjBdtjtjB||	|
�}t|�t|	�}t|
�}ttd�}t�|ddt�|�}t|�t|�}t�|�}t� |�}t|�t� |�}t|�t!�"|�dW|�r�t�#|�|�rt�#|�|�rt�#|�|�r,t�#|�|�r<t�#|�|�rVt�$|�t�#|�|�rft%�&|�|�rvt�#|�S|�r�t�#|�|�r�t�#|�|�r�t�#|�|�r�t�#|�|�r�t�$|�t�#|�|�r�t%�&|�|�r�t�#|�n�|�rt�#|�|�rt�#|�|�r$t�#|�|�r4t�#|�|�rDt�#|�|�r^t�$|�t�#|�|�rnt%�&|�|�r~t�#|�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
            �z-bit_size must be greater than or equal to 512r�z+bit_size must be less than or equal to 4096�@rz!bit_size must be a multiple of 64Nrr�r�r�r�r�Fr�rSrV)'�
isinstancer5�	TypeErrorrr2r�rr$r)r�r+rr�r�r�r�r�r�r�r�r r&rr�r�r%Z
CSSM_ALGID_DHr�r�r(rcrdr�r�loadrjr�r�r�)r�r�r�r�rlr�r�r�r�r�r�r�r�r�r�r�r�rnr�rmrorPrPrQ�generate_dh_parameters�s�
�



�


�













�






�







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��Asn1Certificater4rr3�open�readr�rr2r�)�source�certificate�frPrPrQr;s



,�r;c	CsV|��}d}z4t�|�}t�tj|�}t||�W|r>t�|�Sn|rPt�|�0dS)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
    N)	r�r)�cf_data_from_bytesr$ZSecCertificateCreateWithDatar(ZkCFAllocatorDefaultrrj)r�r��	cf_sourcerKrPrPrQr��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
    Nr�zP
                    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
r3r�r4r�rr2r�r�rrk)r��passwordZprivate_objectr�rPrPrQr=�s(



�
(
�
r=cCs�t|t�r|}nht|t�r$t|�}nTt|t�rft|d��}t|���}Wd�qx1sZ0Ynttdt	|����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
            )r�rr4rr3r�r�r�rr2rk)r�rZr�rPrPrQr>�s



,�r>c
Cs�|jdkrB|j\}}|dkr$td��|tgd��vr�ttd���nF|jdkrh|jdkrhttd|j���n |jdkr�|jd	ur�ttd
���t|t	�r�|jdkr�|�
�}d|d
d
<|��}n|��}tj
}nt|���}tj}d	}d	}d	}�z�t�|�}ttd�}	t|	tj�ttd�}
t|
|�ttd�}t�tjg�}ttd�}t|�}
d|
_d|
_t�|
_t�|
_t�|
_t�|
_ t�|
_!||
_"t�#|t�|	|
d|t�|�}t$|�t|�}t�%|�}|dk�r�t�&|d�}t�'|�|tj
k�r"t(||�W|�rt�)|�|�rt�)|�|�r t�)|�S|tjk�rjt*||�W|�rHt�)|�|�rXt�)|�|�rht�)|�SW|�r|t�)|�|�r�t�)|�|�r�t�)|�n2|�r�t�)|�|�r�t�)|�|�r�t�)|�0d	S)aN
    Common code to load public and private keys into PublicKey and PrivateKey
    objects

    :param key_object:
        An asn1crypto.keys.PublicKeyInfo or asn1crypto.keys.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 key is incompatible with the OS crypto library
        OSError - when an error is returned by the OS crypto library

    :return:
        A PublicKey or PrivateKey object
    r^r_z-OS X only supports EC keys using named curvesr�z�
                OS X only supports EC keys using the named curves secp256r1,
                secp384r1 and secp521r1
                r[Zsha2z�
            OS X only supports DSA keys based on SHA1 (2048 bits or less) - this
            key is based on SHA2 and is %s bits
            Nzz
            The DSA key does not contain the necessary p, q and g parameters
            and can not be used
            r}rTrUz
uint32_t *zCFArrayRef *z"SecItemImportExportKeyParameters *r)+rUrir,r�rr�r�r-r�rrfr�r%ZkSecItemTypePublicKeyrZkSecItemTypePrivateKeyr)r�rr$r"ZkSecFormatOpenSSLr(Zcf_array_from_listZkSecAttrIsExtractabler�version�flagsr r�Z
alertTitleZalertPromptZ	accessRefZkeyUsageZ
keyAttributesZ
SecItemImportr&ZCFArrayGetCountZCFArrayGetValueAtIndexZCFRetainrArjr@)Z
key_objectZ
curve_type�detailsZtemp_key_objectr�Z	item_typer�Z
keys_arrayZ
attr_arrayZformat_pointerZtype_pointerZkeys_pointerZimport_export_params_pointerZimport_export_paramsr��lengthrKrPrPrQrks�

���







�
�







�



�

�

rkcCs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
    )rr=)�datar�rPrPrQr?�sr?c	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|��qSrP)r�)�.0�inforPrPrQ�
<listcomp>��zload_pkcs12.<locals>.<listcomp>)r�r3r�r4r�rr2r�r�r?rkr�)	r�r�r�Zkey_infoZ	cert_infoZextra_certs_inforp�certZextra_certsrPrPrQr<�s2


�
(
�r<cCs�t|ttf�s ttdt|����t|t�s<ttdt|����|j}t|�}t	t
d|�}t
�|jt
j|t|�||�}t|�t|t|��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
    ��
            certificate_or_public_key must be an instance of the Certificate or
            PublicKey class, not %s
            �<
            data must be a byte string, not %s
            �size_t *)r�rrAr�rr2r4�	byte_sizerrr$�
SecKeyEncryptrKr%�kSecPaddingPKCS1�lenr&rr)�certificate_or_public_keyr��
key_length�buffer�
output_lengthrnrPrPrQrE�s.�
��rEcCs�t|t�sttdt|����t|t�s8ttdt|����|j}t|�}tt	d|�}t
dkrbtj}ntj
}t	�|j||t|�||�}t|�t|t|��}t
dkr�t||�}|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
    zV
            private_key must an instance of the PrivateKey class, not %s
            r�r�)r��)r�r@r�rr2r4r�rrr$r'r%�kSecPaddingNoner��
SecKeyDecryptrKr�r&rrr1)rW�
ciphertextr�r�r��paddingrn�outputrPrPrQrD&s:
�
��
rDcCst||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
    )�_encryptr$�kSecPaddingOAEPKey)r�r�rPrPrQrCdsrCcCst||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
    )�_decryptr$r�)rWr�rPrPrQrB|srBc	Cs(t|ttf�s ttdt|����t|t�s<ttdt|����|sHtd��d}d}z�t�	|�}t
td�}t�
|j|�}t|�|r�t�|tj||�t|�t�|tj||�t|�t�||�}t|�t�|�W|r�t�|�|r�t�|�S|�r$t�|�n"|�rt�|�|�r"t�|�0dS)a>
    Encrypts plaintext using an RSA public key or certificate

    :param certificate_or_public_key:
        A Certificate or PublicKey object

    :param data:
        The plaintext - a byte string

    :param padding:
        The padding mode to use, specified as a kSecPadding*Key value

    :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 ciphertext
    r�r��padding must be specifiedN�CFErrorRef *)r�rrAr�rr2r4r�r)r�rr(r$ZSecEncryptTransformCreaterKr*�SecTransformSetAttribute�kSecPaddingKey�kSecTransformInputAttributeName�SecTransformExecuterdrj)r�r�r��cf_data�
sec_transform�
error_pointerr�rPrPrQr��sj�
�

���


��
r�c	Cs t|t�sttdt|����t|t�s8ttdt|����|sDtd��d}d}z�t�|�}t	t
d�}t�|j
|�}t|�t�|tj||�t|�t�|tj||�t|�t�||�}t|�t�|�W|r�t
�|�|r�t
�|�S|�rt
�|�n"|�r
t
�|�|�rt
�|�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, specified as a kSecPadding*Key value

    :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
    �Y
            private_key must be an instance of the PrivateKey class, not %s
            zB
            ciphertext must be a byte string, not %s
            r�Nr�)r�r@r�rr2r4r�r)r�rr(r$ZSecDecryptTransformCreaterKr*r�r�r�r�rdrj)rWr�r�r�r�r��	plaintextrPrPrQr��sh
�
�

���


��
r�cCs |jdkrtd��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
    rT�*The key specified is not an RSA public key�rUr��_verify�r��	signaturer��hash_algorithmrPrPrQrG9s
rGcCs�t|ttf�s ttdt|����t|t�s<ttdt|����|j}|dkrZ|dkrZtd��dddd	d
d��	|d�}|j
}t|�}tt
d
|�}t
�|jtj|t|�||�}	t|	�t|t|��}
t|||j||
�s�td��dS)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
    r�r�rTr}r���� �0r���sha1�sha224�sha256�sha384�sha512rr��Signature is invalidN)r�rrAr�rr2r4rUr��getr�rrr$r�rKr%r�r�r&rrr0r�r.)r�r�r�r�Zcp_algo�hash_lengthr�r�r�rnr�rPrPrQrI[sH�
����rIcCs |jdkrtd��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
    r[z)The key specified is not a DSA public keyr�r�rPrPrQr7�s
r7cCs |jdkrtd��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
    r^z)The key specified is not an EC public keyr�r�rPrPrQr9�s
r9c	Cst|ttf�s ttdt|����t|t�s<ttdt|����t|t�sXttdt|����tgd��}|jdkr||tdg�O}||vr�d}|jdkr�|d7}t	td	|t
|����|jdk�r8|dk�r8t|�|jd
kr�t	td|jt|����t
�|jtj|t|�|t|��}|tjk�s$|tjk�r,td��t|�d
Sd
}d
}d
}	d
}
�z�ttd�}t�|�}t
�|j||�}
t|�t
jt
jt
jt
jt
jt
jd�|}t
�|
t
j ||�t|�|tgd��v�r�ddddd�|}
t�!|
�}	t
�|
t
j"|	|�t|�|jdk�r$t
�|
t
j#t
j$|�t|�t�|�}t
�|
t
j%||�t|�t
�&|
|�}t'|��sxt(|�}t'|��sxtd��t)t�*|��}|�s�td��W|
�r�t�+|
�|�r�t�+|�|�r�t�+|�|	�rt�+|	�nB|
�r�t�+|
�|�r�t�+|�|�rt�+|�|	�rt�+|	�0d
S)a�
    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"

    :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���md5r�r�rrrrT�raw�5"md5", "sha1", "sha224", "sha256", "sha384", "sha512"�, "raw"�B
            hash_algorithm must be one of %s, not %s
            �z�
                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
                rNr��r�rrr��r�r�r�),r�rrAr�rr2r4r�rUr�r�r�r�r$ZSecKeyRawVerifyrKr%r�ZerrSecVerifyFailedZerrSSLCryptor.r&rr(r)r�ZSecVerifyTransformCreater*�
kSecDigestMD5�kSecDigestSHA1�kSecDigestSHA2r��kSecDigestTypeAttribute�cf_number_from_integer�kSecDigestLengthAttributer��kSecPaddingPKCS1Keyr�r�r!r�boolZCFBooleanGetValuerj)r�r�r�r��valid_hash_algorithms�valid_hash_algorithms_errorrn�cf_signaturer��cf_hash_lengthr�r��
hash_constantrr��errorrPrPrQr��s�
�
�

��
�	

���	���
��
�





�


r�cCs|jdkrtd��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
    rT�+The key specified is not an RSA private key�rUr��_sign�rWr�r�rPrPrQrF�s
rFc
Cs�t|t�sttdt|����t|t�s8ttdt|����|j}|dkrV|dkrVtd��dddd	d
d��|d�}t	|||j
|�}|j}t|�}t
td
|�}t�|jtj|t|�||�}	t|	�t|t|��S)a6
    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
    r�r�rTr}rr�r�r�r�r�r�rr�)r�r@r�rr2r4rUr�rr/r�r�rrr$r�rKr%r�r�r&rr)
rWr�r�Zpk_algorZencoded_datar�r�r�rnrPrPrQrH�sF
�
����rHcCs|jdkrtd��t|||�S)aI
    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
    r[z*The key specified is not a DSA private keyrr rPrPrQr6�s
r6cCs|jdkrtd��t|||�S)aL
    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^z*The key specified is not an EC private keyrr rPrPrQr8s
r8c	Cs�t|t�sttdt|����t|t�s8ttdt|����tgd��}|jdkr\|tdg�O}||vr�d}|jdkrz|d7}ttd|t	|����|jdk�r|dk�rt
|�|jd	kr�ttd
|jt
|����|j}t|�}t
td|�}t�|jtj|t
|�||�}t|�t|t|��Sd}	d}
d}d}�z�t
td
�}
t�|j|
�}t|
�tjtjtjtjtjtjd�|}t�|tj||
�t|
�|tgd��v�r�ddddd�|}t�|�}t�|tj ||
�t|
�|jdk�r�t�|tj!tj"|
�t|
�t�#|�}
t�|tj$|
|
�t|
�t�%||
�}	t|
�t�&|	�W|�rJt�'|�|	�rZt�'|	�|
�rjt�'|
�|�rzt�'|�S|	�r�t�'|	�|
�r�t�'|
�|�r�t�'|�nB|�r�t�'|�|	�r�t�'|	�|
�r�t�'|
�|�r�t�'|�0dS)aX
    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"

    :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�rrTrr	r
rrz�
                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
                r�Nr�r
rr�r�r�)(r�r@r�rr2r4r�rUr�r�r�r�rrr$Z
SecKeyRawSignrKr%r�r&rrr(ZSecSignTransformCreater*rrrr�rr)rrr�rr�r�r�rdrj)rWr�r�rrr�r�r�rnrr�rr�r�rrrPrPrQr6s�
�
�

��
�
��	���
��
�




�

�


r)NN)N)N)N)^�
__future__rrrr�base64rr�r�r�Z_asn1rr�r	r
rrr
rrrZ_asymmetricrrrrrrrrr�_errorsrZ_ffirrrrrr r!r"Z	_securityr$r%r&r'Z_core_foundationr(r)r*�utilr+�errorsr,r-r.Z_pkcs1r/r0r1�_typesr2r3r4r5�__all__r@rAr:r�r;r�r=r>rkr?r<rErDrCrBr�r�rGrIr7r9r�rFrHr6r8rrPrPrPrQ�<module>s\,,(u o
5'
8(|

=6>TR"I5"H