o
    c'                     @   s&  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
dZG dd dZG dd dZG dd deZG dd deZedddZG dd deeZG dd deeZG dd dZ eee ej!e" ej!e"< dS )zJOSE utilities.    N)HashableMapping)
ModuleType)AnyCallableIteratorListTupleTypeVarUnioncast)ecrsa)cryptofuncreturnc                 C   s   t t| S N)classmethodabcabstractmethod)r    r   8/opt/certbot/lib/python3.10/site-packages/josepy/util.pyabstractclassmethod      r   c                   @   s   e Zd ZdZdeejejf ddfddZde	de
fdd	Zejfd
edefddZde
defddZdefddZde	fddZdS )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                 C   s&   t |tjst |tjsJ || _d S r   )
isinstancer   X509X509Reqr   selfr   r   r   r   __init__   s   
zComparableX509.__init__namec                 C      t | j|S r   )getattrr   r    r"   r   r   r   __getattr__      zComparableX509.__getattr__filetypec                 C   s*   t | jtjrt|| jS t|| 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(   r   r   r   _dump"   s   zComparableX509._dumpotherc                 C   s    t || jstS |  | kS r   )r   	__class__NotImplementedr+   r    r,   r   r   r   __eq__4   s   zComparableX509.__eq__c                 C   s   t | j|  fS r   )hashr-   r+   r    r   r   r   __hash__9      zComparableX509.__hash__c                 C      d | jj| jS Nz<{0}({1!r})>)formatr-   __name__r   r2   r   r   r   __repr__<   r4   zComparableX509.__repr__)r8   
__module____qualname____doc__r   r   r   r   r!   strr   r&   FILETYPE_ASN1intbytesr+   boolr0   r3   r9   r   r   r   r   r      s    r   c                   @   s   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dZdedefdd	Zd
edefddZdefddZdddZdS )ComparableKeyznComparable wrapper for ``cryptography`` keys.

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

    r3   r   c                 C   s
   || _ d S r   )_wrappedr   r   r   r   r!   H   s   
zComparableKey.__init__r"   r   c                 C   r#   r   )r$   rC   r%   r   r   r   r&   P   r'   zComparableKey.__getattr__r,   c                 C   s\   t || jr| jj|jjurtS t| jdr|  | kS t| jdr,|  | kS tS )Nprivate_numberspublic_numbers)r   r-   rC   r.   hasattrrD   rE   r/   r   r   r   r0   S   s   zComparableKey.__eq__c                 C   r5   r6   )r7   r-   r8   rC   r2   r   r   r   r9   ^   r4   zComparableKey.__repr__c                 C   s(   t | jtjtjfr| S | | j S )zGet wrapped public key.)r   rC   r   RSAPublicKeyWithSerializationr   'EllipticCurvePublicKeyWithSerializationr-   
public_keyr2   r   r   r   rI   a   s
   
zComparableKey.public_keyN)r   rB   )r8   r:   r;   r<   r.   r3   r   r?   __annotations__r   r   RSAPrivateKeyWithSerializationrG   r   (EllipticCurvePrivateKeyWithSerializationrH   r!   r=   r   r&   rA   r0   r9   rI   r   r   r   r   rB   @   s    
 
rB   c                   @      e Zd ZdZdefddZdS )ComparableRSAKeyzWrapper for ``cryptography`` RSA keys.

    Wraps around:

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

    r   c              	   C   st   t | jtjr"|  }|j}t| j|j|j	|j
|j|j|j|jfS t | jtjr7|  }t| j|j|jfS t r   )r   rC   r   rK   rD   rE   r1   r-   pqdmp1dmq1iqmpnerG   NotImplementedErrorr    privpubr   r   r   r3   t   s   zComparableRSAKey.__hash__Nr8   r:   r;   r<   r?   r3   r   r   r   r   rN   j   s    	rN   c                   @   rM   )ComparableECKeyzWrapper for ``cryptography`` RSA keys.
    Wraps around:
    - :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey`
    - :class:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey`
    r   c                 C   sp   t | jtjr|  }|j}t| j|jj	|j
|j|jfS t | jtjr5|  }t| j|jj	|j
|jfS t r   )r   rC   r   rL   rD   rE   r1   r-   curver"   xyprivate_valuerH   rV   rW   r   r   r   r3      s   zComparableECKey.__hash__NrZ   r   r   r   r   r[      s    r[   GenericImmutableMapImmutableMap)boundc                   @   s   e Zd ZU dZdZeedf ed< 	 deddfdd	Z	d
e
dede
fddZdedefddZdee fddZdefddZdefddZdededdfddZdefddZdS )ra   z5Immutable key to value mapping with attribute access.r   .	__slots__kwargsr   Nc                 K   s`   t |t | jkrtdd| j|rd|d| jD ]}t| ||| q!d S )NzA__init__() takes exactly the following arguments: {0} ({1} given), none)setrc   	TypeErrorr7   joinobject__setattr__pop)r    rd   slotr   r   r   r!      s   
zImmutableMap.__init__r    c                 K   s   i | |}t | di |S )zReturn updated map.Nr   )type)r    rd   itemsr   r   r   update   s   zImmutableMap.updatekeyc                 C   s$   zt | |W S  ty   t|w r   )r$   AttributeErrorKeyErrorr    rq   r   r   r   __getitem__   
   zImmutableMap.__getitem__c                 C   
   t | jS r   )iterrc   r2   r   r   r   __iter__      
zImmutableMap.__iter__c                 C   rw   r   )lenrc   r2   r   r   r   __len__   rz   zImmutableMap.__len__c                    s   t t fdd jD S )Nc                 3   s    | ]}t  |V  qd S r   )r$   ).0rm   r2   r   r   	<genexpr>   s    z(ImmutableMap.__hash__.<locals>.<genexpr>)r1   tuplerc   r2   r   r2   r   r3      s   zImmutableMap.__hash__r"   valuec                 C      t dNzcan't set attributerr   r    r"   r   r   r   r   rk         zImmutableMap.__setattr__c                 C   s$   d | jjddd |  D S )Nz{0}({1})re   c                 s        | ]\}}d  ||V  qdS z	{0}={1!r}Nr7   r}   rq   r   r   r   r   r~      s
    

z(ImmutableMap.__repr__.<locals>.<genexpr>)r7   r-   r8   ri   ro   r2   r   r   r   r9      s   
zImmutableMap.__repr__)r8   r:   r;   r<   rc   r	   r=   rJ   r   r!   r`   rp   ru   r   ry   r?   r|   r3   rk   r9   r   r   r   r   ra      s   
 	c                   @   s   e Zd ZdZdZdededdfddZd	edefd
dZde	e fddZ
defddZdeeeef df fddZdefddZdedefddZdededdfddZdefddZdS )
frozendictzFrozen dictionary.)_items_keysargsrd   r   Nc                 O   sh   |r	|s	t |}nt|dkrt|d tr|d }nt t| d| t| dtt|	  d S )N   r   r   r   )
dictr{   r   r   rh   rj   rk   r   sortedkeys)r    r   rd   ro   r   r   r   r!      s   

zfrozendict.__init__rq   c                 C   s
   | j | S r   )r   rt   r   r   r   ru      rz   zfrozendict.__getitem__c                 C   rw   r   )rx   r   r2   r   r   r   ry      rz   zfrozendict.__iter__c                 C   rw   r   )r{   r   r2   r   r   r   r|      rz   zfrozendict.__len__.c                    s   t  fdd jD S )Nc                 3   s    | ]	}| | fV  qd S r   r   )r}   rq   r2   r   r   r~      s    z+frozendict._sorted_items.<locals>.<genexpr>)r   r   r2   r   r2   r   _sorted_items   s   zfrozendict._sorted_itemsc                 C   s   t |  S r   )r1   r   r2   r   r   r   r3      r'   zfrozendict.__hash__r"   c                 C   s$   z| j | W S  ty   t|w r   )r   rs   rr   r%   r   r   r   r&      rv   zfrozendict.__getattr__r   c                 C   r   r   r   r   r   r   r   rk      r   zfrozendict.__setattr__c                 C   s   d ddd |  D S )Nzfrozendict({0})re   c                 s   r   r   r   r   r   r   r   r~      s    z&frozendict.__repr__.<locals>.<genexpr>)r7   ri   r   r2   r   r   r   r9      s   
zfrozendict.__repr__)r8   r:   r;   r<   rc   r   r!   r=   ru   r   ry   r?   r|   r	   r   r3   r&   rk   r9   r   r   r   r   r      s    r   c                   @   sn   e Zd ZdZdeddfddZdedefdd	Zded
eddfddZ	deddfddZ
dee fddZdS )_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                 C   s   || j d< d S N_module)__dict__)r    r   r   r   r   r!      r   z_UtilDeprecationModule.__init__attrc                 C   s$   |dkrt jdtd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   r   r   r   r&      s
   z"_UtilDeprecationModule.__getattr__r   c                 C   s   t | j|| d S r   )setattrr   )r    r   r   r   r   r   rk     r4   z"_UtilDeprecationModule.__setattr__c                 C   s   t | j| d S r   )delattrr   r   r   r   r   __delattr__     z"_UtilDeprecationModule.__delattr__c                 C   s   dgt | j S r   )dirr   r2   r   r   r   __dir__  r   z_UtilDeprecationModule.__dir__)r8   r:   r;   r<   r   r!   r=   r   r&   rk   r   r   r   r   r   r   r   r      s    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   rB   rN   r[   r`   ra   r   r   modulesr8   r   r   r   r   <module>   s&    (.*,1