ok

Mini Shell

Direktori : /lib/python3.6/site-packages/josepy/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/josepy/__pycache__/jwk.cpython-36.pyc

3

�}:ay4�@sdZddlZddlZddlZddlZddlmZmZmZm	Z	m
Z
ddlZddl
mZddlmZddlmZddlmZddlmZdd	lmZmZmZeje�ZGd
d�dejejd�ZejGd
d�de��Z ejGdd�de��Z!ejGdd�de��Z"dS)z
JSON Web Key.�N)�Dict�Optional�Sequence�Type�Union)�default_backend)�hashes)�
serialization)�ec)�rsa)�errors�	json_util�utilc@s�eZdZUdZdZiZeeeffZ	e
e	eZe
edddd�Z
eeeeee
eeff
ejfdd	�Zejd
d��Zeddd
��Zeddd��ZdS)�JWKz
JSON Web Key.ZktyN�,�:T)�indentZ
separatorsZ	sort_keyscsJtj|�t�d�}|jtj�fdd��j�j�D�f�j�j	��|j
�S)zgCompute JWK Thumbprint.

        https://tools.ietf.org/html/rfc7638

        :returns: bytes

        )�backendcs i|]\}}|�jkr||�qS�)�required)�.0�k�v)�selfr�/usr/lib/python3.6/jwk.py�
<dictcomp>4sz"JWK.thumbprint.<locals>.<dictcomp>)rZHashr�update�json�dumpsZto_json�items�_thumbprint_json_dumps_params�encode�finalize)rZ
hash_functionZdigestr)rr�
thumbprint*s
zJWK.thumbprintcCs
t��dS)ziGenerate JWK with public key.

        For symmetric cryptosystems, this would return ``self``.

        N)�NotImplementedError)rrrr�
public_key9szJWK.public_keycCs�|dkrt�n|}i}xVtjtjfD]F}y||||�Stttjjfk
rh}z|||<WYdd}~Xq$Xq$WxRtj	tj
fD]B}y
|||�Sttjjfk
r�}z|||<WYdd}~Xq|Xq|Wtjdj
|���dS)NzUnable to deserialize key: {0})rr	Zload_pem_private_keyZload_der_private_key�
ValueError�	TypeError�cryptography�
exceptionsZUnsupportedAlgorithmZload_pem_public_keyZload_der_public_keyr�Error�format)�cls�data�passwordrr)�loader�errorrrr�_load_cryptography_keyBs"
zJWK._load_cryptography_keycCs�y|j|||�}Wn4tjk
rF}ztjd|�t|d�Sd}~XnX|jtk	rvt||j	�rvtjdj
|j|j���x(|jj
�D]}t||j	�r�||d�Sq�Wtjdj
|j���dS)a�Load serialized key as JWK.

        :param str data: Public or private key serialized as PEM or DER.
        :param str password: Optional password.
        :param backend: A `.PEMSerializationBackend` and
            `.DERSerializationBackend` provider.

        :raises errors.Error: if unable to deserialize, or unsupported
            JWK algorithm

        :returns: JWK of an appropriate type.
        :rtype: `JWK`

        z,Loading symmetric key, asymmetric failed: %s)�keyNz"Unable to deserialize {0} into {1}zUnsupported algorithm: {0})r1rr*�logger�debug�JWKOct�typ�NotImplemented�
isinstance�cryptography_key_typesr+�	__class__�TYPES�values)r,r-r.rr2r0Zjwk_clsrrr�load\szJWK.load)rr)NN)NN)�__name__�
__module__�__qualname__�__doc__�type_field_namer;r�strrr9rr7rr rr�int�boolrZSHA256r#�abc�abstractmethodr%�classmethodr1r=rrrrrs
&	r)�	metaclassc@s>eZdZdZdZdZdejfZdd�Z	e
dd��Zd	d
�ZdS)
r5zSymmetric JWK.�octr2rcCsdtj|j�iS)Nr)r
�encode_b64joser2)rrrr�fields_to_partial_json�szJWKOct.fields_to_partial_jsoncCs|tj|d�d�S)Nr)r2)r
�decode_b64jose)r,�jobjrrr�fields_from_json�szJWKOct.fields_from_jsoncCs|S)Nr)rrrrr%�szJWKOct.public_keyN)r2)
r>r?r@rAr6�	__slots__rrBrrLrHrOr%rrrrr5|s
r5csteZdZdZdZejejfZdZ	de
jdfZ�fdd�Z
edd	��Zed
d��Zdd
�Zedd��Zdd�Z�ZS)�JWKRSAz�RSA JWK.

    :ivar key: :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey`
        or :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey` wrapped
        in :class:`~josepy.util.ComparableRSAKey`

    ZRSAr2�e�ncs>d|kr,t|dtj�r,tj|d�|d<t�j||�dS)Nr2)r8rZComparableRSAKey�super�__init__)r�args�kwargs)r:rrrU�s
zJWKRSA.__init__cCs0t|j�d�}tj|d�}tj|jd|d��S)zOEncode Base64urlUInt.
        :type data: long
        :rtype: unicode
        ��big)�	byteorder�length)�max�
bit_length�mathZceilr
rK�to_bytes)r,r-r[rrr�
_encode_param�szJWKRSA._encode_paramcCsFy$tj|�}|stj��tj|dd�Stk
r@tj��YnXdS)zDecode Base64urlUInt.rY)rZN)r
rMr�DeserializationErrorrD�
from_bytesr&)r,r-�binaryrrr�
_decode_param�s
zJWKRSA._decode_paramcCst|�|jj�d�S)N)r2)�typer2r%)rrrrr%�szJWKRSA.public_keyc
sB��fdd�dD�\}}tj||d�}d�kr>|jt��}�n��j�d�}d�ks|d�ks|d	�ks|d
�ks|d�ks|d�kr�t�fd
d�dD��\}}}	}
}}tdd�|D��r�tjdj|���t�fdd�|D��\}}}	}
}n6tj	|||�\}}tj
||�}	tj||�}
tj||�}tj
||||	|
||�jt��}�|d�S)Nc3s|]}�j�|�VqdS)N)rd)r�x)r,rNrr�	<genexpr>�sz*JWKRSA.fields_from_json.<locals>.<genexpr>rSrR)rRrS�d�p�q�dp�dq�qiZothc3s|]}�j|�VqdS)N)�get)rrf)rNrrrg�scss|]}|dkr|VqdS)Nr)rZparamrrrrg�sz(Some private parameters are missing: {0}c3s|]}�j|�VqdS)N)rd)rrf)r,rrrg�s)r2)rSrR)rirjrkrlrm)rZRSAPublicNumbersr%rrd�tuplerr*r+Zrsa_recover_prime_factorsZrsa_crt_dmp1Zrsa_crt_dmq1Zrsa_crt_iqmpZRSAPrivateNumbers�private_key)
r,rNrSrR�public_numbersr2rhrirjrkrlrmZ
all_paramsr)r,rNrrO�s." zJWKRSA.fields_from_jsonc	s~t�jjtj�r*�jj�}|j|jd�}n>�jj�}�jj	�j�}|j|j|j
|j|j|j
|j|jd�}�fdd�|j�D�S)N)rSrR)rSrRrhrirjrkrlrmcsi|]\}}�j|�|�qSr)r`)rr2�value)rrrr�sz1JWKRSA.fields_to_partial_json.<locals>.<dictcomp>)r8r2�_wrappedr�RSAPublicKeyrqrSrR�private_numbersr%rhrirjZdmp1Zdmq1Ziqmpr)rZnumbers�params�private�publicr)rrrL�s 



zJWKRSA.fields_to_partial_json)r2)r>r?r@rAr6rrtZ
RSAPrivateKeyr9rPrrBrrUrHr`rdr%rOrL�
__classcell__rr)r:rrQ�s
'rQcs�eZdZdZdZdZejejfZ	de
jddfZ�fdd�Z
ed	d
��Zedd��Zed
d��Zedd��Zedd��Zdd�Zedd��Zdd�Z�ZS)�JWKECz�EC JWK.

    :ivar key: :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey`
        or :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey` wrapped
        in :class:`~josepy.util.ComparableRSAKey`

    ZECr2�crvrf�ycs>d|kr,t|dtj�r,tj|d�|d<t�j||�dS)Nr2)r8rZComparableECKeyrTrU)rrVrW)r:rrrU
s
zJWKEC.__init__cCstj|jd|d��S)zlEncode Base64urlUInt.
        :type data: long
        :type key_size: long
        :rtype: unicode
        rY)rZr[)r
rKr_)r,r-r[rrrr`szJWKEC._encode_paramcCs`y>tj|�}t|�|kr2tjdj||t|�d���tj|dd�Stk
rZtj��YnXdS)zDecode Base64urlUInt.ziExpected parameter "{name}" to be {valid_lengths} bytes after base64-decoding; got {length} bytes instead)�nameZ
valid_lengthsr[rY)rZN)	r
rM�lenrrar+rDrbr&)r,r-r}Zvalid_lengthrcrrrrds
zJWKEC._decode_paramcCs0|dkrdS|dkrdS|dkr$dStj��dS)NZ	secp256r1zP-256Z	secp384r1zP-384Z	secp521r1zP-521)r�SerializationError)r,Z
curve_namerrr�_curve_name_to_crv+szJWKEC._curve_name_to_crvcCs<|dkrtj�S|dkr tj�S|dkr0tj�Stj��dS)NzP-256zP-384zP-521)r
�	SECP256R1�	SECP384R1�	SECP521R1rra)r,r{rrr�
_crv_to_curve5szJWKEC._crv_to_curvecCs4t|tj�rdSt|tj�r dSt|tj�r0dSdS)N� �0�B)r8r
r�r�r�)r,�curverrr�expected_length_for_curve@szJWKEC.expected_length_for_curvecs�i}t�jjtj�r �jj��n>t�jjtj�rT�jj�}�jj�j��|j	|d<n
t
jd���j|d<�j
|d<��fdd�|j�D�}�j�jj�|d<|S)NrhzRSupplied key is neither of type EllipticCurvePublicKey nor EllipticCurvePrivateKeyrfr|cs&i|]\}}�j|�j�j��|�qSr)r`r�r�)rr2rr)rxrrrrVsz0JWKEC.fields_to_partial_json.<locals>.<dictcomp>r{)r8r2rsr
�EllipticCurvePublicKeyrq�EllipticCurvePrivateKeyrur%Z
private_valuerrrfr|rr�r�r})rrvrwr)rxrrrLIs


zJWKEC.fields_to_partial_jsoncs��j�d�}�j|�����fdd�d	D�\}}tj|||d�}d�krX|jt��}n&�j�dd��}tj||�jt��}�|d�S)
Nr{c3s |]}�j�||��VqdS)N)rd)rrS)r,�expected_lengthrNrrrg_sz)JWKEC.fields_from_json.<locals>.<genexpr>rfr|)rfr|r�rh)r2)rfr|)	r�r�r
ZEllipticCurvePublicNumbersr%rrdZEllipticCurvePrivateNumbersrp)r,rNr�rfr|rqr2rhr)r,r�rNrrOZs
zJWKEC.fields_from_jsoncCs8t|jd�r|jj�}n|jj�jt��}t|�|d�S)Nr%)r2)�hasattrr2r%rqrre)rr2rrrr%iszJWKEC.public_key)r2)r>r?r@rAr6rPr
r�r�r9rrBrrUrHr`rdr�r�r�rLrOr%ryrr)r:rrz�s	
	rz)#rArFrZloggingr^ZtypingrrrrrZcryptography.exceptionsr(Zcryptography.hazmat.backendsrZcryptography.hazmat.primitivesrr	Z)cryptography.hazmat.primitives.asymmetricr
rZjosepyrr
rZ	getLoggerr>r3ZTypedJSONObjectWithFields�ABCMetar�registerr5rQrzrrrr�<module>s(
gk

Zerion Mini Shell 1.0