HEX
Server: LiteSpeed
System: Linux shams.tasjeel.ae 5.14.0-611.5.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Nov 11 08:09:09 EST 2025 x86_64
User: infowars (1469)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: //usr/lib/python3.9/site-packages/jwcrypto/__pycache__/jwk.cpython-39.pyc
a

���f���@s�ddlZddlmZmZddlmZddlmZddlm	Z	ddl
mZddlm
Z
mZddlmZdd	lmZzdd
lmZWney�dd�ZYn0dd
lmZddlmZmZddlmZmZGdd�d�ZgZzddlmZmZe� d�Wne�yeZeZYn0zddl!m"Z"m#Z#e� d�Wne�yPeZ"eZ#Yn0z8ddl$m%Z%m&Z&e'e&dd�Z(e(du�r~e�e� d�Wne�y�eZ%eZ&Yn0zddl)m*Z*m+Z+e� d�Wne�y�eZ*eZ+Yn0edd�Z,edd�Z-edd�Z.edd�Z/e,ee�e-e"e#�e.e%e&�e/e*e+�d�Z0dddd d!�Z1Gd"d#�d#e�Z2ed$d%�Z3e3d&d'd'e2j4�e3d(d'd'e2j5�e3d)d'd'e2j5�e3d*d+d+e2j5�d,�e3d-d'd'e2j5�e3d.d'd'e2j6�e3d/d+d+e2j6�e3d0d+d+e2j6�e3d1d+d+e2j6�e3d2d+d+e2j6�e3d3d+d+e2j6�e3d4d+d+e2j6�e3d5d+d+e2j7�d6�	d7e3d8d+d'e2j5�ie3d&d'd'e2j4�e3d9d'd'e2j5�e3d:d+d+e2j5�d;�d!�Z8e3d<d'dd�e3d=d'dd�e3d>d'dd�e3d?d'dd�e3d@d'dd�e3dAd'dd�e3dBd'dd�e3dCd'dd�e3dDd'dd�dE�	Z9dFdGdHdIdJdKdLdMdNdOdPdQ�Z:dRdSdT�Z;dUdVdWdXdYdZd[d\d]�Z<d^d_d`dadbdcddde�Z=e
�>�ddddde
�?�e
�@�e
�A�e
�B�e
�C�e
�D�e
�Edf�de
�Fdg�dh�ZGGdidj�dje�ZHGdkdl�dle�ZIGdmdn�dne�ZJGdodp�dpe�ZKGdqdr�dreL�ZMGdsdt�dteN�ZOGdudv�dveL�ZPdS)w�N)�hexlify�	unhexlify)�
namedtuple)�Enum)�x509)�default_backend)�hashes�
serialization)�ec)�rsa)�
deprecatedcGsdd�}|S)NcSs|S�N�)�funcrr�0/usr/lib/python3.9/site-packages/jwcrypto/jwk.py�innerszdeprecated.<locals>.innerr)Z_fn�argsrrrrrsr)�JWException)�base64url_decode�base64url_encode)�json_decode�json_encodec@s0eZdZedd��Zedd��Zedd��ZdS)�UnimplementedOKPCurveKeycCst�dSr
��NotImplementedError)�clsrrr�generatesz!UnimplementedOKPCurveKey.generatecGst�dSr
r�rrrrr�from_public_bytes"sz*UnimplementedOKPCurveKey.from_public_bytescGst�dSr
rrrrr�from_private_bytes&sz+UnimplementedOKPCurveKey.from_private_bytesN)�__name__�
__module__�__qualname__�classmethodrrrrrrrrs

r)�Ed25519PublicKey�Ed25519PrivateKey�Ed25519)�Ed448PublicKey�Ed448PrivateKey�Ed448)�X25519PublicKey�X25519PrivateKeyr�X25519)�
X448PublicKey�X448PrivateKey�X448zpubkey privkey)r&r)r,r/zElliptic Curve�RSAzOctet sequencezOctet Key Pair)�ECr0�oct�OKPc@seZdZdZdZdZdZdS)�ParmTypezA string with a namezBase64url EncodedzBase64urlUint EncodedzUnsupported ParameterN)r r!r"�name�b64�b64u�unsupportedrrrrr4lsr4Z	Parameterz description public required typeZCurveTzX CoordinatezY CoordinatezECC Private KeyF)�crv�x�y�dZModulus�ExponentzPrivate ExponentzFirst Prime FactorzSecond Prime FactorzFirst Factor CRT ExponentzSecond Factor CRT ExponentzFirst CRT CoefficientzOther Primes Info)	�n�er<�p�q�dp�dq�qiZoth�kz	Key Valuez
Public KeyzPrivate Key)r9r:r<zKey TypezPublic Key UsezKey OperationsZ	AlgorithmzKey IDz	X.509 URLzX.509 Certificate Chainz"X.509 Certificate SHA-1 Thumbprintz$X.509 Certificate SHA-256 Thumbprint)	�kty�use�key_ops�alg�kidZx5uZx5cZx5tzx5t#S256zP-256 curvezP-384 curvezP-521 curvezSECG secp256k1 curvez%Ed25519 signature algorithm key pairsz#Ed448 signature algorithm key pairszX25519 function key pairszX448 function key pairsz\BrainpoolP256R1 curve (unregistered, custom-defined in breach of IETF rules by gematik GmbH)z\BrainpoolP384R1 curve (unregistered, custom-defined in breach of IETF rules by gematik GmbH)z\BrainpoolP512R1 curve (unregistered, custom-defined in breach of IETF rules by gematik GmbH))�P-256�P-384�P-521�	secp256k1r&r)r,r/�BP-256�BP-384�BP-512zDigital Signature or MACZ
Encryption)�sig�encz Compute digital Signature or MACzVerify digital signature or MACzEncrypt contentz6Decrypt content and validate decryption, if applicablezEncrypt keyz2Decrypt key and validate decryption, if applicablez
Derive keyz#Derive bits not to be used as a key)�sign�verify�encrypt�decrypt�wrapKey�	unwrapKey�	deriveKey�
deriveBitsrKrLrMrNrOrPrQ)Z	secp256r1Z	secp384r1Z	secp521r1rNZbrainpoolP256r1ZbrainpoolP384r1ZbrainpoolP512r1� �@)�sha-256zsha-256-128zsha-256-120z
sha-256-96z
sha-256-64z
sha-256-32zsha-384zsha-512zsha3-224zsha3-256zsha3-384zsha3-512zblake2s-256zblake2b-256zblake2b-512cs*eZdZdZd�fdd�	Zdd�Z�ZS)�InvalidJWKTypezfInvalid JWK Type Exception.

    This exception is raised when an invalid parameter type is used.
    Ncstt|���||_dSr
)�superr_�__init__�value)�selfrb��	__class__rrra�szInvalidJWKType.__init__cCsd|jtt���fS)Nz&Unknown type "%s", valid types are: %s)rb�list�JWKTypesRegistry�keys�rcrrr�__str__�s�zInvalidJWKType.__str__)N�r r!r"�__doc__rarj�
__classcell__rrrdrr_�sr_cs(eZdZdZ�fdd�Zdd�Z�ZS)�InvalidJWKUsagez�Invalid JWK usage Exception.

    This exception is raised when an invalid key usage is requested,
    based on the key type and declared usage constraints.
    cstt|���||_||_dSr
)r`rnrarbrG)rcrGrbrdrrra�szInvalidJWKUsage.__init__cCs\|jtt���vrt|j}n
d|j}|jtt���vrFt|j}n
d|j}d||fS)N�Unknown(%s)z.Invalid usage requested: "%s". Valid for: "%s")rGrf�JWKUseRegistryrhrb)rc�usage�validrrrrjs

�zInvalidJWKUsage.__str__rkrrrdrrn�srncs(eZdZdZ�fdd�Zdd�Z�ZS)�InvalidJWKOperationz�Invalid JWK Operation Exception.

    This exception is raised when an invalid key operation is requested,
    based on the key type and declared usage constraints.
    cstt|���||_||_dSr
)r`rsra�op�values)rc�	operationrurdrrraszInvalidJWKOperation.__init__cCsr|jtt���vrt|j}n
d|j}g}|jD]2}|tt���vrV|�t|�q2|�d|�q2d||fS)Nroz2Invalid operation requested: "%s". Valid for: "%s")rtrf�JWKOperationsRegistryrhru�append)rcrtrr�vrrrrjs

�zInvalidJWKOperation.__str__rkrrrdrrssrsc@seZdZdZdS)�InvalidJWKValuez�Invalid JWK Value Exception.

    This exception is raised when an invalid/unknown value is used in the
    context of an operation that requires specific values to be used based
    on the key type or other constraints.
    N)r r!r"rlrrrrrz*srzcs�eZdZdZ�fdd�Zedd��Zdd�Zd�d	d
�Zdd�Z	d�d
d�Z
dd�Zdd�Zdd�Z
d�dd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zed'd(��Zd�d+d,�Zd�d-d.�Zd/d0�Zd�d1d2�Zd�d3d4�Zd�d5d6�Zd7d8�Zed9d:��Z ed;d<��Z!ed=d>��Z"ee#d?�d@dA���Z$ee#d?�dBdC���Z%ee#d?�dDdE���Z&e#d?�dFdG��Z'dHdI�Z(dJdK�Z)dLdM�Z*dNdO�Z+dPdQ�Z,dRdS�Z-dTdU�Z.dVdW�Z/dXdY�Z0dZd[�Z1d\d]�Z2d^d_�Z3d�d`da�Z4d�dbdc�Z5d�ddde�Z6dfdg�Z7d�dhdi�Z8d�djdk�Z9edldm��Z:ed�dndo��Z;e<�=�fdpdq�Z>d�dsdt�Z?�fdudv�Z@dwdx�ZAd�dydz�ZB�fd{d|�ZCd}d~�ZDdd��ZEd�d��ZF�fd�d��ZGed�d���ZHd�d��ZI�ZJS)��JWKz�JSON Web Key object

    This object represents a Key.
    It must be instantiated by using the standard defined key/value pairs
    as arguments of the initialization function.
    csLtt|���d|_d|_d|vr4|jfi|��n|rH|jfi|��dS)a�Creates a new JWK object.

        The function arguments must be valid parameters as defined in the
        'IANA JSON Web Key Set Parameters registry' and specified in
        the :data:`JWKParamsRegistry` variable. The 'kty' parameter must
        always be provided and its value must be a valid one as defined
        by the 'IANA JSON Web Key Types registry' and specified in the
        :data:`JWKTypesRegistry` variable. The valid key parameters per
        key type are defined in the :data:`JWKValuesRegistry` variable.

        To generate a new random key call the class method generate() with
        the appropriate 'kty' parameter, and other parameters as needed (key
        size, public exponents, curve types, etc..)

        Valid options per type, when generating new keys:
         * oct: size(int)
         * RSA: public_exponent(int), size(int)
         * EC: crv(str) (one of P-256, P-384, P-521, secp256k1)
         * OKP: crv(str) (one of Ed25519, Ed448, X25519, X448)

        Deprecated:
        Alternatively if the 'generate' parameter is provided with a
        valid key type as value then a new key will be generated according
        to the defaults or provided key strength options (type specific).

        :param \**kwargs: parameters (optional).

        :raises InvalidJWKType: if the key type is invalid
        :raises InvalidJWKValue: if incorrect or inconsistent parameters
            are provided.
        Nr)r`r{ra�_cache_pub_k�_cache_pri_k�generate_key�
import_key)rc�kwargsrdrrra;s zJWK.__init__c
Ksd|�}d}z|d}t|d|�}Wn2ttfyV}zt|�|�WYd}~n
d}~00||�|S)NrF�_generate_%s)�getattr�KeyError�AttributeErrorr_)rr��objrF�genr?rrrrds zJWK.generatec
Ks`d}z|�d�}t|d|�}Wn2ttfyR}zt|�|�WYd}~n
d}~00||�dS)Nrr�)�popr�r�r�r_)rc�paramsrFr�r?rrrr~ps
 zJWK.generate_keyNc
Csx|}d|vr|�d�}n\d|vrtzddlm}|�|d�}Wn.tyl}ztd�|�WYd}~n
d}~00|j}|S)N�sizerIr)�JWAzInvalid 'alg' parameter)r�Zjwcrypto.jwar�Zinstantiate_algr��
ValueErrorZ
input_keysize)rcr�Zdefault_sizer�r�rIr?rrr�
_get_gen_sizezs zJWK._get_gen_sizecCsB|�|d�}t�|d�}d|d<t|�|d<|jfi|��dS)N��r2rFrE)r��os�urandomrr)rcr�r��keyrrr�
_generate_oct�s
zJWK._generate_octcCsfd}|dur|ddd}t|��d��d�}t|�}||krJ||8}n|d}tt|d|��S)Nr�r���L�0x�0)�hex�rstrip�lstrip�lenrr)rc�iZbit_size�extendZhexiZhexlrrr�_encode_int�s
zJWK._encode_intcCsHd}|�|d�}d|vr"|�d�}t�||t��}|j|fi|��dS)NiiZpublic_exponent)r�r�r�generate_private_keyr�_import_pyca_pri_rsa)rcr�Zpubexpr�r�rrr�
_generate_RSA�s
zJWK._generate_RSAcKs||��}|jd|�|jj�|�|jj�|�|j�|�|j�|�|j�|�|j	�|�|j
�|�|j�d�	|jfi|��dS)Nr0)	rFr>r?r<r@rArBrCrD)
�private_numbers�updater��public_numbersr>r?r<r@rAZdmp1Zdmq1Ziqmpr�rcr�r��pnrrrr��s





�zJWK._import_pyca_pri_rsacKs<|��}|jd|�|j�|�|j�d�|jfi|��dS)Nr0)rFr>r?)r�r�r�r>r?rr�rrr�_import_pyca_pub_rsa�s

�zJWK._import_pyca_pub_rsacCs|�d�}|dur|}n|dkr&d}n|}|rV|}|dkr>d}||krVtd||f��|�d�}|dur�|dur�||kr�td||f��|dkr�t��S|dkr�t��S|d	kr�t��S|dkr�t��S|d
kr�t��S|dkr�t�	�S|dk�r�t�
�S|tv�rt|Std
|��dS)Nr9zP-256KrNz.Curve requested is "%s", but key curve is "%s"rFz>Curve Requested is of type "%s", but key curve is of type "%s"rKrLrMrOrPrQzUnknown Curve Name [%s])�getrzr_r
Z	SECP256R1Z	SECP384R1Z	SECP521R1Z	SECP256K1ZBrainpoolP256R1ZBrainpoolP384R1ZBrainpoolP512R1�_OKP_CURVES_TABLE)rcr5�ctyper9ZcnameZccrvrFrrr�_get_curve_by_name�sH
�
�

zJWK._get_curve_by_namecCsXd}d|vr|�d�}d|vr(|�d�}|�|d�}t�|t��}|j|fi|��dS)NrK�curver9r1)r�r�r
r�r�_import_pyca_pri_ec)rcr�r��curve_fnr�rrr�_generate_EC�s

zJWK._generate_ECc	Ksd|��}|jjj}|jdt|jj|�|jj|�|�|jj	|�|�|j
|�d�|jfi|��dS)Nr1)rFr9r:r;r<)r�r�r��key_sizer��JWKpycaCurveMapr5r�r:r;Z
private_valuer�rcr�r�r�r�rrrr��s

�zJWK._import_pyca_pri_eccKsR|��}|jj}|jdt|jj|�|j|�|�|j|�d�|j	fi|��dS)Nr1)rFr9r:r;)
r�r�r�r�r�r5r�r:r;rr�rrr�_import_pyca_pub_ecs
�zJWK._import_pyca_pub_eccCs@d|vrtd��|�|dd�}|j��}|j|fi|��dS)Nr9z)Must specify "crv" for OKP key generationr3)rzr��privkeyr�_import_pyca_pri_okp)rcr�r�r�rrr�
_generate_OKPs

zJWK._generate_OKPcCs<t��D]"\}}t||j|jf�r|Sqtd|��dS)NzInvalid OKP Key object %r)r��items�
isinstance�pubkeyr�rz)rcr�r5�valrrr�_okp_curve_from_pyca_keys
zJWK._okp_curve_from_pyca_keyc
Ks^|jd|�|�t|�tjjtjjt����t|�	��
tjjtjj��d�|jfi|��dS)Nr3)rFr9r<r:)
r�r�r�
private_bytesr	�Encoding�Raw�
PrivateFormat�NoEncryption�
public_key�public_bytes�PublicFormatr�rcr�r�rrrr�s�
��zJWK._import_pyca_pri_okpcKs>|jd|�|�t|�tjjtjj��d�|jfi|��dS)Nr3)rFr9r:)	r�r�rr�r	r�r�r�rr�rrr�_import_pyca_pub_okp&s��zJWK._import_pyca_pub_okpcKs�i}d}d|_d|_t|���}tt���D],}||vr,||||<||vr,|�|�qDq,|�d�}|tvrtt|��tt	|���D]4}||vr�||||<|d7}||vr�|�|�q�q�t	|�
�D�]\}}|jr�||vr�td|��|j
tjk�r||v�rtd|��|j
tjk�rl||v�rlzt||�Wn4t�yj}ztd|�|�WYd}~n
d}~00|j
tjkr�||vr�z|�||�Wq�t�y�}ztd|�|�WYd}~q�d}~00q�|D]}||||<�q�|dk�r�td��d	|v�rF|d	D]>}	d}
|d	D]}|	|k�r|
d7}
�q|
dk�rtd
���qd|v�r�d	|v�r�dd
g}gd�}
|ddk�r�|
D]}||d	v�r|td���q|n0|ddk�r�|D]}||d	v�r�td���q�|��|�d|d�|�|�dS)NrrF�zMissing required value %szUnsupported parameter %s�"%s" is not base64url encoded�!"%s" is not Base64urlUInt encodedzNo Key Values foundrHzDuplicate values in "key_ops"rGrTrU)rVrWrXrYrZr[rRzBIncompatible "use" and "key_ops" values specified at the same timerS)r|r}rfrh�JWKParamsRegistry�remover�rgr_�JWKValuesRegistryr��requiredrz�typer4r8r6r�	Exceptionr7�_decode_int�clear�__setitem__r�)rcr�ZnewkeyZkey_vals�namesr5rFr�r?�koZcntZckoZsiglZenclrtrrrr0s�
����



zJWK.import_keyc
CsR|�}zt|�}Wn*ty<}zt|�WYd}~n
d}~00|jfi|��|S)z�Creates a RFC 7517 JWK from the standard JSON format.

        :param key: The RFC 7517 representation of a JWK.

        :return: A JWK object that holds the json key.
        :rtype: JWK
        N)rr�rzr)rr�r�Zjkeyr?rrr�	from_json�s	z
JWK.from_jsonTFcCs|dur|�|�S|�|�S)a�Exports the key in the standard JSON format.
        Exports the key regardless of type, if private_key is False
        and the key is_symmetric an exception is raised.

        :param private_key(bool): Whether to export the private key.
                                  Defaults to True.

        :return: A portable representation of the key.
            If as_dict is True then a dictionary is returned.
            By default a json string
        :rtype: `str` or `dict`
        T)�_export_all�
export_public)rc�private_key�as_dictrrr�export�s

z
JWK.exportcCs|��}|dur|St|�S)a�Exports the public key in the standard JSON format.
        It fails if one is not available like when this function
        is called on a symmetric key.

        :param as_dict(bool): If set to True export as python dict not JSON

        :return: A portable representation of the public key only.
            If as_dict is True then a dictionary is returned.
            By default a json string
        :rtype: `str` or `dict`
        T)�_public_paramsr)rcr��pubrrrr��szJWK.export_publiccCsx|jstd��i}t}|D](}||jr||��vr|�|�||<qt|�d�}|D]}||jrV|�|�||<qV|S)N�No public key availablerF)�
has_publicr_r��publicrhr�r�)rcr��regr5rrrr��s

zJWK._public_paramscCs"i}|�|�|dur|St|�S)NT)r�r)rcr�r<rrrr��s

zJWK._export_allcCs|jr|�|�Std��dS)a�Export the private key in the standard JSON format.
        It fails for a JWK that has only a public key or is symmetric.

        :param as_dict(bool): If set to True export as python dict not JSON

        :return: A portable representation of a private key.
            If as_dict is True then a dictionary is returned.
            By default a json string
        :rtype: `str` or `dict`
        �No private key availableN)�has_privater�r_�rcr�rrr�export_private�s
zJWK.export_privatecCs|jr|�|�Std��dS)NzNot a symmetric key)�is_symmetricr�r_r�rrr�export_symmetric�s
zJWK.export_symmetriccCs|��}tfi|��Sr
)r�r{)rcr�rrrr��sz
JWK.publiccCsB|jr
dSt|�d�}|D] }||jr||��vrdSqdS)z4Whether this JWK has an asymmetric Public key value.FrFT�r�r�r�r�rh�rcr�r5rrrr��szJWK.has_publiccCsB|jr
dSt|�d�}|D] }||js||��vrdSqdS)z5Whether this JWK has an asymmetric Private key value.FrFTr�r�rrrr��szJWK.has_privatecCs|�d�dkS)z$Whether this JWK is a symmetric key.rFr2�r�rirrrr�szJWK.is_symmetric�cCs
|�d�S)zThe Key typerFr�rirrr�key_typeszJWK.key_typecCs
|�d�S)z^The Key ID.
        Provided by the kid parameter if present, otherwise returns None.
        rJr�rirrr�key_idsz
JWK.key_idcCs |�d�dvrtd��|�d�S)zThe Curve Name.rF)r1r3zNot an EC or OKP keyr9)r�r_rirrr�	key_curvesz
JWK.key_curvecCs
|�|�S)a0Gets the Elliptic Curve associated with the key.

        :param arg: an optional curve name

        :raises InvalidJWKType: the key is not an EC or OKP key.
        :raises InvalidJWKValue: if the curve name is invalid.

        :return: An EllipticCurve object
        :rtype: `EllipticCurve`
        )r�)rc�argrrr�	get_curvesz
JWK.get_curvecCsT|�d�}|r ||kr t||��|�d�}|rPt|t�s>|g}||vrPt||��dS)NrGrH)r�rnr�rfrs)rcrqrvrG�opsrrr�_check_constraints*s



zJWK._check_constraintscCsttt|��d�S)N�)�intrr)rcr>rrrr�6szJWK._decode_intcCs,|�|�d��}|�|�d��}t�||�S)Nr?r>)r�r�rZRSAPublicNumbers)rcr?r>rrr�
_rsa_pub_n9szJWK._rsa_pub_nc
Csz|�|�d��}|�|�d��}|�|�d��}|�|�d��}|�|�d��}|�|�d��}t�|||||||���S)Nr@rAr<rBrCrD)r�r�rZRSAPrivateNumbersr�)rcr@rAr<rBrCrDrrr�
_rsa_pri_n>szJWK._rsa_pri_ncCs(|j}|dur$|���t��}||_|Sr
)r|r�r�r�rcrErrr�_rsa_pubGs
zJWK._rsa_pubcCs(|j}|dur$|���t��}||_|Sr
)r}r�r�rr�rrr�_rsa_priNs
zJWK._rsa_pricCs<|�|�d��}|�|�d��}|j|dd�}t�|||�S)Nr:r;r1)r�)r�r�r�r
ZEllipticCurvePublicNumbers)rcr�r:r;r�rrr�	_ec_pub_nUsz
JWK._ec_pub_ncCs"|�|�d��}t�||�|��S)Nr<)r�r�r
ZEllipticCurvePrivateNumbersr�)rcr�r<rrr�	_ec_pri_n[sz
JWK._ec_pri_ncCs*|j}|dur&|�|��t��}||_|Sr
)r|r�r�r�rcr�rErrr�_ec_pub_s
zJWK._ec_pubcCs*|j}|dur&|�|��t��}||_|Sr
)r}r�r�rr�rrr�_ec_prifs
zJWK._ec_pric
Cs||j}|durx|�d�}zt|j}Wn2tyX}ztd|�|�WYd}~n
d}~00t|�d��}|�|�}||_|S)Nr9�Unknown curve "%s"r:)r|r�r�r�r�rzrr)rcrEr9r�r?r:rrr�_okp_pubms
$
zJWK._okp_pubc
Cs||j}|durx|�d�}zt|j}Wn2tyX}ztd|�|�WYd}~n
d}~00t|�d��}|�|�}||_|S)Nr9r�r<)r}r�r�r�r�rzrr)rcrEr9r�r?r<rrr�_okp_pri{s
$
zJWK._okp_pricCsV|�d�}|dkr|�d�S|dkr,|��S|dkr>|�|�S|dkrN|��St�dS�NrFr2rEr0r1r3)r�r�r�r�r�rcr�Zktyperrr�_get_public_key�s


zJWK._get_public_keycCsV|�d�}|dkr|�d�S|dkr,|��S|dkr>|�|�S|dkrN|��St�dSr)r�r�r�r�rrrrr�_get_private_key�s


zJWK._get_private_keycCs�|�dtt����}|tur"|g}|durN|�d�dkrB|�d�St||��n�|dkrl|�d|�|�|�S|dkr�|�d|�|�|�S|d	ks�|d
kr�|�d|�|�|�S|dks�|d
kr�|�d|�|�|�St�dS)aGet the key object associated to the requested operation.
        For example the public RSA key for the 'verify' operation or
        the private EC key for the 'decrypt' operation.

        :param operation: The requested operation.
         The valid set of operations is available in the
         :data:`JWKOperationsRegistry` registry.
        :param arg: An optional, context specific, argument.
         For example a curve name.

        :raises InvalidJWKOperation: if the operation is unknown or
         not permitted with this key.
        :raises InvalidJWKUsage: if the use constraints do not permit
         the operation.

        :return: A Python Cryptography key object for asymmetric keys
            or a baseurl64_encoded octet string for symmetric keys
        rHNrFr2rErTrRrUrVrXrSrWrY)	r�rfrwrhrsr�rrr)rcrvr�Zvalidopsrrr�
get_op_key�s,
�




zJWK.get_op_keycCs�t|tj�r|�|�n�t|tj�r0|�|�ntt|tj�rH|�|�n\t|tj	�r`|�
|�nDt|ttt
f�r||�|�n(t|tttf�r�|�|�ntd|��dS)NzUnknown key object %r)r�rZ
RSAPrivateKeyr�ZRSAPublicKeyr�r
ZEllipticCurvePrivateKeyr�ZEllipticCurvePublicKeyr�r%r(r+r�r$r'r*r�rz�rcr�rrr�import_from_pyca�s&��zJWK.import_from_pycacCs�ztj||t�d�}Wn�ty�}zt|dur2|�ztj|t�d�}WnFty�ztj|t�d�}|��}Wnty�|�Yn0Yn0WYd}~n
d}~00|�|�|dur�|�	�}|�
d|�dS)atImports a key from data loaded from a PEM file.
        The key may be encrypted with a password.
        Private keys (PKCS#8 format), public keys, and X509 certificate's
        public keys can be imported with this interface.

        :param data(bytes): The data contained in a PEM file.
        :param password(bytes): An optional password to unwrap the key.
        )�password�backendN�rrJ)r	Zload_pem_private_keyrr�Zload_pem_public_keyrZload_pem_x509_certificater�r�
thumbprintr�)rc�datarrJr�r?Zcertrrr�import_from_pem�s.
�
�
�&
zJWK.import_from_pemcCs�tjj}|rx|jstd��tjj}|dur4t��}n0t|t	�rJt�
|�}n|dur\td��ntd��|�
�j|||d�S|js�td��tjj}|��j||d�SdS)	aqExports keys to a data buffer suitable to be stored as a PEM file.
        Either the public or the private key can be exported to a PEM file.
        For private keys the PKCS#8 format is used. If a password is provided
        the best encryption method available as determined by the cryptography
        module is used to wrap the key.

        :param private_key: Whether the private key should be exported.
         Defaults to `False` which means the public key is exported by default.
        :param password(bytes): A password for wrapping the private key.
         Defaults to False which will cause the operation to fail. To avoid
         encryption the user must explicitly pass None, otherwise the user
         needs to provide a password in a bytes buffer.

        :return: A serialized bytes buffer containing a PEM formatted key.
        :rtype: `bytes`
        r�NFz+The password must be None or a bytes stringz!The password string must be bytes)�encoding�formatZencryption_algorithmr�)r
r)r	r�ZPEMr�r_r�ZPKCS8r�r��bytesZBestAvailableEncryptionr��	TypeErrorrr�r�r�ZSubjectPublicKeyInforr�)rcr�rrS�fZenc_algrrr�
export_to_pems&


�zJWK.export_to_pemcCs|�}|�|�|Sr
)r)rr�r�rrr�	from_pyca(s
z
JWK.from_pycacCs|�}|�||�|S)a?Creates a key from PKCS#8 formatted data loaded from a PEM file.
           See the function `import_from_pem` for details.

        :param data(bytes): The data contained in a PEM file.
        :param password(bytes): An optional password to unwrap the key.

        :return: A JWK object.
        :rtype: JWK
        )r)rrrr�rrr�from_pem.szJWK.from_pemcCspd|�d�i}t|d��D]\}}|jr|�|�||<qtj|t�d�}|�tt	|��
d���t|���S)z�Returns the key thumbprint as specified by RFC 7638.

        :param hashalg: A hash function (defaults to SHA256)

        :return: A base64url encoded digest of the key
        :rtype: `str`
        rFr	�utf8)
r�r�r�r�rZHashrr�rr�encoder�finalize)rcZhashalg�tr5r�Zdigestrrrr
=s	zJWK.thumbprintr^c
Csnzt|}Wn4ty@}ztd�|��|�WYd}~n
d}~00|durXtd�|���|�|�}d�||�S)adReturns the key thumbprint URI as specified by RFC 9278.

        :param hname: A hash function name as specified in IANA's
         Named Information registry:
         https://www.iana.org/assignments/named-information/
         Values from `IANANamedInformationHashAlgorithmRegistry`

        :return: A JWK Thumbprint URI
        :rtype: `str`
        zUnknown hash "{}"NzUnsupported hash "{}"z*urn:ietf:params:oauth:jwk-thumbprint:{}:{})�)IANANamedInformationHashAlgorithmRegistryr�rzrr
)rcZhname�hr?rrrr�thumbprint_uriNs&
zJWK.thumbprint_uric
s�|�d�}|dkrP|dur@|tvr*t|��tt|��||�dS||krPtd��|tt|�	��v�rTd|_
d|_t||jt
jkr�z(t|�}|dkr�|dkr�|dkr�t�Wn2ty�}ztd|�|�WYd}~n
d}~00nZt||jt
jk�r>z|�|�Wn4t�y<}ztd|�|�WYd}~n
d}~00tt|��||�dS|tt�	��v�r|tt|��||�dStt�	��D]:}||k�r��q�|tt|�	��v�r�td�||����q�tt|��||�dS)	NrFzCannot change key type�r2rEr�r�z Cannot set '{}' on '{}' key type)r�rgr_r`r{r�r�rfr�rhr|r}r�r4r6rr�rzr7r�r�r�r)rc�itemrbrFryr?r5rdrrr�esZ
����
�
zJWK.__setitem__cOs,t|i|����D]\}}|�||�qdS�zV
        :param \*args: arguments
        :param \**kwargs: keyword arguments
        N��dictr�r��rcrr�rEryrrrr��sz
JWK.updatecCs"||��vr|�||�|�|�Sr
�rhr�r��rcr��defaultrrr�
setdefault�szJWK.setdefaultcs�|�|�}|durt|��|dkrNtt|���D]}|�|�dur2td��q2|�d�}|dur�|tt|���vr�d|_d|_tt|��	|�dS)NrFz#Cannot remove 'kty', values present)
r�r�rfr�rhr|r}r`r{�__delitem__)rcrZparamr5rFrdrrr&�s


zJWK.__delitem__cCs2t|t�stS|��|��ko0|�d�|�d�kS�NrJ)r�r{�NotImplementedr
r�)rc�otherrrr�__eq__�s

�z
JWK.__eq__cCst|��|�d�f�Sr')�hashr
r�rirrr�__hash__�szJWK.__hash__cCs�zj|t��vr&||��vr&|�|�WS|�d�}|durd|tt|���vrd||��vrd|�|�WSt�Wnty�t|�d�Yn0dS)NrF)r�rhr�rfr�r�r�)rcrrFrrr�__getattr__�s
zJWK.__getattr__cs�z`|t��vr|�||�tt���D]$}|tt|���vr&|�||�q&tt|��||�Wnt	y|t
|�d�Yn0dSr
)r�rhr�rfrgr�r`r{�__setattr__r�r�)rcrrbr5rdrrr.�szJWK.__setattr__c
Csd|�}ddi}zt|�d��|d<Wn*tyN}zt|�WYd}~n
d}~00|jfi|��|S)z�Creates a symmetric JWK key from a user password.

        :param password: A password in utf8 format.

        :return: a JWK object
        :rtype: JWK
        rFr2rrEN)rrr�rzr)rrr�r�r?rrr�
from_password�s	zJWK.from_passwordcCs(i}|�dd�|d<|��|d<t|�S)NrJzMissing Key IDr
)r�r
r)rc�	repr_dictrrr�__repr__�szJWK.__repr__)N)N)N)TF)F)F)F)F)N)N)NN)NN)FF)N)r^)N)Kr r!r"rlrar#rr~r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�r�r�r�r�r�r�r��propertyr�r�r�rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrrrrr�SHA256r
rr�r�r%r&r*r,r-r.r/r1rmrrrdrr{3s�)




	
.
W












	


*
!
&

9

r{c@seZdZdd�ZdS)�_JWKkeyscCs"t|t�std��t�||�dS)z�Adds a JWK object to the set

        :param elem: the JWK object to add.

        :raises TypeError: if the object is not a JWK.
        z#Only JWK objects are valid elementsN)r�r{r�set�add�rc�elemrrrr6�s
z_JWKkeys.addN)r r!r"r6rrrrr4�sr4cs�eZdZdZ�fdd�Zdd�Zdd�Z�fdd	�Zd
d�Zdd
d�Z	dd�Z
d dd�Zdd�Ze
dd��Zdd�Zdd�Zdd�Z�ZS)!�JWKSetz�A set of JWK objects.

    Inherits from the standard 'dict' builtin type.
    Creates a special key 'keys' that is of a type derived from 'set'
    The 'keys' attribute accepts only :class:`jwcrypto.jwk.JWK` elements.
    cs6tt|���tt|��dt��|j|i|��dS�Nrh)r`r9rar�r4r�)rcrr�rdrrraszJWKSet.__init__cCs|d��Sr:)�__iter__rirrrr;szJWKSet.__iter__cCs|d�|�Sr:)�__contains__rrrrr<szJWKSet.__contains__cs8|dkr"t|t�s"|d�|�ntt|��||�dSr:)r�r4r6r`r9r�)rcr�r�rdrrr�szJWKSet.__setitem__cOs,t|i|����D]\}}|�||�qdSrrr!rrrr�!sz
JWKSet.updateNcCs"||��vr|�||�|�|�Sr
r"r#rrrr%)szJWKSet.setdefaultcCs|d�|�dSr:)r6r7rrrr6.sz
JWKSet.addTFcCs`i}|��D]>\}}|dkrBg}|D]}|�|j|dd��q$|}|||<q|durX|St|�S)aExports a RFC 7517 key set.
           Exports as json by default, or as dict if requested.

        :param private_key(bool): Whether to export private keys.
                                  Defaults to True.
        :param as_dict(bool): Whether to return a dict instead of
                              a JSON object

        :return: A portable representation of the key set.
            If as_dict is True then a dictionary is returned.
            By default a json string
        :rtype: `str` or `dict`
        rhT)r�)r�rxr�r)rcZprivate_keysr�Zexp_dictrEryrh�jwkrrrr�1s
z
JWKSet.exportc
Cs�zt|�}Wn*ty6}zt|�WYd}~n
d}~00d|vrDt�|��D]<\}}|dkr�|D]}|d�tfi|���q`qL|||<qLdS)z�Imports a RFC 7517 key set using the standard JSON format.

        :param keyset: The RFC 7517 representation of a JOSE key set.
        Nrh)rr�rzr�r6r{)rc�keysetZjwksetr?rEryr=rrr�
import_keysetKszJWKSet.import_keysetcCs|�}|�|�|S)z�Creates a RFC 7517 key set from the standard JSON format.

        :param keyset: The RFC 7517 representation of a JOSE key set.

        :return: A JWKSet object.
        :rtype: JWKSet
        )r?)rr>r�rrrr�_s	
zJWKSet.from_jsoncCsF|�|�}t|�dkrtd��zt|�dWSty@YdS0dS)z�Gets a key from the set.
        :param kid: the 'kid' key identifier.

        :return: A JWK from the set
        :rtype: JWK
        r�z3Duplicate keys found with requested kid: 1 expectedrN)�get_keysr�rz�tuple�
IndexError)rcrJrhrrr�get_keyls
�zJWKSet.get_keycs�fdd�|dD�S)z�Gets keys from the set with matching kid.
        :param kid: the 'kid' key identifier.

        :return: a List of keys
        :rtype: `list`
        csh|]}|�d��kr|�qS�rJr�)�.0r�rDrr�	<setcomp>�rz"JWKSet.get_keys.<locals>.<setcomp>rhr)rcrJrrDrr@|szJWKSet.get_keyscCsNi}|��D]8\}}|dkr<g}|D]}|�t|��q$|}|||<qt|�Sr:)r�rx�reprr)rcr0rEryrhr=rrrr1�s
zJWKSet.__repr__)N)TF)r r!r"rlrar;r<r�r�r%r6r�r?r#r�rCr@r1rmrrrdrr9s


	r9)Qr�Zbinasciirr�collectionsr�enumrZcryptographyrZcryptography.hazmat.backendsrZcryptography.hazmat.primitivesrr	Z)cryptography.hazmat.primitives.asymmetricr
rZtyping_extensionsr�ImportErrorZjwcrypto.commonrrrrrrZImplementedOkpCurvesZ1cryptography.hazmat.primitives.asymmetric.ed25519r$r%rxZ/cryptography.hazmat.primitives.asymmetric.ed448r'r(Z0cryptography.hazmat.primitives.asymmetric.x25519r*r+r�Z
priv_bytesZ.cryptography.hazmat.primitives.asymmetric.x448r-r.Z_Ed25519_CURVEZ_Ed448_CURVEZ
_X25519_CURVEZ_X448_CURVEr�rgr4ZJWKParameterr5r6r7r8r�r�ZJWKEllipticCurveRegistryrprwr�r3ZSHA384ZSHA512ZSHA3_224ZSHA3_256ZSHA3_384ZSHA3_512ZBLAKE2sZBLAKE2brr_rnrsrzr r{r5r4r9rrrr�<module>sL








�	�

���������"�������	�	N