
    eA'                        d Z ddlZddlZddlZddl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 ddlmZmZ ddlmZ ded	efd
Z G d d      Z G d d      Z G d de      Z G d de      Z edd      Z G d dee      Z G d dee      Z G d d      Z  ee e ejB                  e"               ejB                  e"<   y)zJOSE utilities.    N)HashableMapping)
ModuleType)AnyCallableIteratorListTupleTypeVarUnioncast)ecrsa)cryptofuncreturnc                 >    t        t        j                  |             S N)classmethodabcabstractmethod)r   s    -/usr/lib/python3/dist-packages/josepy/util.pyabstractclassmethodr      s    s))$/00    c                       e Zd ZdZdeej                  ej                  f   ddfdZde	de
fdZej                  fdedefd	Zd
e
defdZdefdZde	fdZy)ComparableX509zWrapper for OpenSSL.crypto.X509** objects that supports __eq__.

    :ivar wrapped: Wrapped certificate or certificate request.
    :type wrapped: `OpenSSL.crypto.X509` or `OpenSSL.crypto.X509Req`.

    wrappedr   Nc                 ~    t        |t        j                        st        |t        j                        sJ || _        y r   )
isinstancer   X509X509Reqr   selfr   s     r   __init__zComparableX509.__init__   s*    '6;;/:gv~~3VVVr   namec                 .    t        | j                  |      S r   )getattrr   r#   r%   s     r   __getattr__zComparableX509.__getattr__   s    t||T**r   filetypec                     t        | j                  t        j                        r t        j                  || j                        S t        j
                  || j                        S )aN  Dumps the object into a buffer with the specified encoding.

        :param int filetype: The desired encoding. Should be one of
            `OpenSSL.crypto.FILETYPE_ASN1`,
            `OpenSSL.crypto.FILETYPE_PEM`, or
            `OpenSSL.crypto.FILETYPE_TEXT`.

        :returns: Encoded X509 object.
        :rtype: bytes

        )r   r   r   r    dump_certificatedump_certificate_request)r#   r*   s     r   _dumpzComparableX509._dump!   sF     dllFKK0**8T\\BB ..xFFr   otherc                 |    t        || j                        st        S | j                         |j                         k(  S r   )r   	__class__NotImplementedr.   r#   r/   s     r   __eq__zComparableX509.__eq__3   s-    %0!!zz|u{{},,r   c                 L    t        | j                  | j                         f      S r   )hashr1   r.   r#   s    r   __hash__zComparableX509.__hash__8   s    T^^TZZ\233r   c                 b    dj                  | j                  j                  | j                        S Nz<{0}({1!r})>)formatr1   __name__r   r7   s    r   __repr__zComparableX509.__repr__;   s#    $$T^^%<%<dllKKr   )r<   
__module____qualname____doc__r   r   r    r!   r$   strr   r)   FILETYPE_ASN1intbytesr.   boolr4   r8   r=    r   r   r   r      s    fkk6>>&A B t + + + %+$8$8 Gc GU G$-C -D -
4# 4L# Lr   r   c                       e Zd ZU dZeZeg ef   ed<   de	e
j                  e
j                  ej                  ej                  f   fdZdedefdZdedefd	Zdefd
ZddZy)ComparableKeyznComparable wrapper for ``cryptography`` keys.

    See https://github.com/pyca/cryptography/issues/2122.

    r8   r   c                     || _         y r   )_wrappedr"   s     r   r$   zComparableKey.__init__H   s      r   r%   r   c                 .    t        | j                  |      S r   )r'   rJ   r(   s     r   r)   zComparableKey.__getattr__S   s    t}}d++r   r/   c                 z   t        || j                        r,| j                  j                  |j                  j                  urt        S t	        | j                  d      r!| j                         |j                         k(  S t	        | j                  d      r!| j                         |j                         k(  S t        S )Nprivate_numberspublic_numbers)r   r1   rJ   r2   hasattrrM   rN   r3   s     r   r4   zComparableKey.__eq__V   s    5$..1}}&&enn.F.FF!!T]]$56'')U-B-B-DDDT]]$45&&(E,@,@,BBB!!r   c                 b    dj                  | j                  j                  | j                        S r:   )r;   r1   r<   rJ   r7   s    r   r=   zComparableKey.__repr__c   s#    $$T^^%<%<dmmLLr   c                     t        | j                  t        j                  t        j
                  f      r| S | j                  | j                  j                               S )zGet wrapped public key.)r   rJ   r   RSAPublicKeyWithSerializationr   'EllipticCurvePublicKeyWithSerializationr1   
public_keyr7   s    r   rT   zComparableKey.public_keyf   sH    MM..0Z0Z[
 K~~dmm66899r   N)r   rH   )r<   r>   r?   r@   r2   r8   r   rC   __annotations__r   r   RSAPrivateKeyWithSerializationrR   r   (EllipticCurvePrivateKeyWithSerializationrS   r$   rA   r   r)   rE   r4   r=   rT   rF   r   r   rH   rH   ?   s     #1Hhr3w0	 ..--77668
	 , , ,"C "D "M# M:r   rH   c                       e Zd ZdZdefdZy)ComparableRSAKeyzWrapper for ``cryptography`` RSA keys.

    Wraps around:

    - :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey`
    - :class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey`

    r   c           
         t        | j                  t        j                        r| j	                         }|j
                  }t        | j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  f      S t        | j                  t        j                        r<| j                         }t        | j                  |j                  |j                  f      S t!               r   )r   rJ   r   rV   rM   rN   r6   r1   pqdmp1dmq1iqmpnerR   NotImplementedErrorr#   privpubs      r   r8   zComparableRSAKey.__hash__{   s     dmmS%G%GH'')D%%CDIItyyRURWRWY\Y^Y^_  s'H'HI%%'C677!##r   Nr<   r>   r?   r@   rC   r8   rF   r   r   rY   rY   q   s    $# $r   rY   c                       e Zd ZdZdefdZy)ComparableECKeyzWrapper for ``cryptography`` EC keys.
    Wraps around:
    - :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey`
    - :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey`
    r   c                    t        | j                  t        j                        rh| j	                         }|j
                  }t        | j                  |j                  j                  |j                  |j                  |j                  f      S t        | j                  t        j                        rQ| j                         }t        | j                  |j                  j                  |j                  |j                  f      S t               r   )r   rJ   r   rW   rM   rN   r6   r1   curver%   xyprivate_valuerS   rb   rc   s      r   r8   zComparableECKey.__hash__   s     dmmR%P%PQ'')D%%CtGYGYZ[[r'Q'QR%%'CFGG!##r   Nrf   rF   r   r   rh   rh      s    $# $r   rh   GenericImmutableMapImmutableMap)boundc                       e Zd ZU dZdZeedf   ed<   	 deddfdZ	d	e
dede
fd
ZdedefdZdee   fdZdefdZdefdZdededdfdZdefdZy)ro   z5Immutable key to value mapping with attribute access.rF   .	__slots__kwargsr   Nc                 R   t        |      t        | j                        k7  rOt        dj                  dj	                  | j                        |rdj	                  |                  d            | j                  D ](  }t
        j                  | ||j                  |             * y )NzA__init__() takes exactly the following arguments: {0} ({1} given), none)setrr   	TypeErrorr;   joinobject__setattr__pop)r#   rs   slots      r   r$   zImmutableMap.__init__   s    v;#dnn--$fIIdnn-Ftyy/@  QW  NN 	=DtT6::d+;<	=r   r#   c                 0    i | |} t        |       di |S )zReturn updated map.rF   )type)r#   rs   itemss      r   updatezImmutableMap.update   s%    #5d#5f#5tDz"E""r   keyc                 L    	 t        | |      S # t        $ r t        |      w xY wr   )r'   AttributeErrorKeyErrorr#   r   s     r   __getitem__zImmutableMap.__getitem__   s-    	 4%% 	 3-	 s    #c                 ,    t        | j                        S r   )iterrr   r7   s    r   __iter__zImmutableMap.__iter__   s    DNN##r   c                 ,    t        | j                        S r   )lenrr   r7   s    r   __len__zImmutableMap.__len__   s    4>>""r   c                 R     t        t         fd j                  D                    S )Nc              3   6   K   | ]  }t        |        y wr   )r'   ).0r}   r#   s     r   	<genexpr>z(ImmutableMap.__hash__.<locals>.<genexpr>   s     I$'$-Is   )r6   tuplerr   r7   s   `r   r8   zImmutableMap.__hash__   s    EI$..IIJJr   r%   valuec                     t        d      Nzcan't set attributer   r#   r%   r   s      r   r{   zImmutableMap.__setattr__       233r   c                     dj                  | j                  j                  dj                  d | j	                         D                    S )Nz{0}({1})ru   c              3   F   K   | ]  \  }}d j                  ||        ywz	{0}={1!r}Nr;   r   r   r   s      r   r   z(ImmutableMap.__repr__.<locals>.<genexpr>   s!     Tek((e4T   !)r;   r1   r<   ry   r   r7   s    r   r=   zImmutableMap.__repr__   s:      NN##IITtzz|TT
 	
r   )r<   r>   r?   r@   rr   r
   rA   rU   r   r$   rn   r   r   r   r   rC   r   r8   r{   r=   rF   r   r   ro   ro      s    ?!#IuS#X#+	= 	= 	=#( #C #<O #
 s  s  $(3- $# #K# K4 4C 4D 4
# 
r   c                       e Zd ZdZdZdededdfdZdedefd	Zde	e   fd
Z
defdZdeeeef   df   fdZdefdZdedefdZdededdfdZdefdZy)
frozendictzFrozen dictionary.)_items_keysargsrs   r   Nc           
      "   |r|st        |      }n1t        |      dk(  rt        |d   t              r|d   }n
t	               t
        j                  | d|       t
        j                  | dt        t        |j                                            y )N   r   r   r   )
dictr   r   r   rx   rz   r{   r   sortedkeys)r#   r   rs   r   s       r   r$   zfrozendict.__init__   so    $LEY!^
47G <GE+ 	4514%uzz|0D*EFr   r   c                      | j                   |   S r   )r   r   s     r   r   zfrozendict.__getitem__   s    {{3r   c                 ,    t        | j                        S r   )r   r   r7   s    r   r   zfrozendict.__iter__   s    DJJr   c                 ,    t        | j                        S r   )r   r   r7   s    r   r   zfrozendict.__len__   s    4;;r   .c                 @     t         fd j                  D              S )Nc              3   ,   K   | ]  }||   f  y wr   rF   )r   r   r#   s     r   r   z+frozendict._sorted_items.<locals>.<genexpr>   s     <#c49%<s   )r   r   r7   s   `r   _sorted_itemszfrozendict._sorted_items   s    <<<<r   c                 4    t        | j                               S r   )r6   r   r7   s    r   r8   zfrozendict.__hash__   s    D&&())r   r%   c                 R    	 | j                   |   S # t        $ r t        |      w xY wr   )r   r   r   r(   s     r   r)   zfrozendict.__getattr__   s0    	';;t$$ 	' &&	's    &r   c                     t        d      r   r   r   s      r   r{   zfrozendict.__setattr__   r   r   c                 l    dj                  dj                  d | j                         D                    S )Nzfrozendict({0})ru   c              3   F   K   | ]  \  }}d j                  ||        ywr   r   r   s      r   r   z&frozendict.__repr__.<locals>.<genexpr>   s!     \ek((e4\r   )r;   ry   r   r7   s    r   r=   zfrozendict.__repr__   s0     ''II\tGYGYG[\\
 	
r   )r<   r>   r?   r@   rr   r   r$   rA   r   r   r   rC   r   r
   r   r8   r)   r{   r=   rF   r   r   r   r      s    #IGc GS GT G s  s   (3-     =uU38_c%9: =*# *' ' '4 4C 4D 4
# 
r   r   c                   f    e Zd ZdZdeddfdZdedefdZdededdfd	Z	deddfd
Z
dee   fdZy)_UtilDeprecationModulez
    Internal class delegating to a module, and displaying warnings when attributes
    related to the deprecated "abstractclassmethod" attributes in the josepy.util module.
    moduler   Nc                 "    || j                   d<   y N_module)__dict__)r#   r   s     r   r$   z_UtilDeprecationModule.__init__  s    #)i r   attrc                 p    |dk(  rt        j                  dt        d       t        | j                  |      S )Nr   zThe abstractclassmethod attribute in josepy.util is deprecated and will be removed soon. Please use the built-in decorators @classmethod and @abc.abstractmethod together instead.   )
stacklevel)warningswarnDeprecationWarningr'   r   r#   r   s     r   r)   z"_UtilDeprecationModule.__getattr__  s7    ((MM8 # t||T**r   r   c                 2    t        | j                  ||       y r   )setattrr   )r#   r   r   s      r   r{   z"_UtilDeprecationModule.__setattr__  s    dE*r   c                 0    t        | j                  |       y r   )delattrr   r   s     r   __delattr__z"_UtilDeprecationModule.__delattr__  s    d#r   c                 4    dgt        | j                        z   S r   )dirr   r7   s    r   __dir__z_UtilDeprecationModule.__dir__  s    {S...r   )r<   r>   r?   r@   r   r$   rA   r   r)   r{   r   r	   r   rF   r   r   r   r     sj    
*z *d *	+ 	+ 	++ +C +D +$ $ $/c /r   r   )#r@   r   sysr   collections.abcr   r   typesr   typingr   r   r   r	   r
   r   r   r   )cryptography.hazmat.primitives.asymmetricr   r   OpenSSLr   r   r   r   rH   rY   rh   rn   ro   r   r   modulesr<   rF   r   r   <module>r      s     
 
  -  M M M = 1h 1; 1*L *LZ/: /:d$} $4$m $* 3>J ,
7H ,
^-
( -
f/ /> Z)?H@U)VWH r   