
    kKjs*                        d dl Zd dlZd dlmZmZ ddlmZmZ ddl	m
Z
mZ ddlmZmZmZ g dZ ej"                  d      Z G d	 d
ej&                  e         Z G d d      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d      Z G d dee      Z G d dee      Z G d dee      Z G d  d!ee      Zej@                  eeeef   Z!y)"    N)ABCMetaabstractmethod   )HeaderHeaderRegistryDict)InvalidKeyTypeErrorInvalidKeyLengthError)KeyECKeyOctKey)	RecipientCompactEncryptionBaseJSONEncryptionGeneralJSONEncryptionFlattenedJSONEncryptionJWEEncModelJWEZipModelKeyManagementJWEDirectEncryptionJWEKeyEncryptionJWEKeyWrappingJWEKeyAgreementJWEAlgModelKeyTypec                       e Zd Z	 	 ddej                  d   dedz  dedz  fdZdefdZd	e	d
ej                  ddfdZde	ddfdZy)r   Nparent)r   r   r   headerrecipient_keyc                 X    || _         || _        || _        d | _        d | _        d | _        y N)_Recipient__parentr   r   
sender_keyencrypted_keyephemeral_key)selfr   r   r   s       h/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/joserfc/_rfc7516/models.py__init__zRecipient.__init__   s1     **.+/-1    returnc                 L   i }t        | j                  t              r;| j                  j                  r%|j	                  | j                  j                         | j
                  r|j	                  | j
                         |j	                  | j                  j                         |S r    )
isinstancer!   r   unprotectedupdater   	protected)r%   rvs     r&   headerszRecipient.headers)   sj    dmm%78T]]=V=VIIdmm//0;;IIdkk"
		$--))*	r(   kvc                     t        | j                  t              r(| j                  j                  j	                  ||i       y | j
                  r| j
                  j	                  ||i       y ||i| _        y r    )r+   r!   r   r.   r-   r   )r%   r1   r2   s      r&   
add_headerzRecipient.add_header3   sU    dmm%67MM##**Aq62[[KK1v&a&DKr(   kidc                 (    | j                  d|       y )Nr5   )r4   )r%   r5   s     r&   set_kidzRecipient.set_kid;   s    s#r(   NN)__name__
__module____qualname__tUnionr   r   r'   r0   strAnyr4   r7    r(   r&   r   r      sz     !%(,	2_`2 2 ~	2 !C !AEE !d !$3 $4 $r(   r   c                       e Zd ZdZddededz  fdZdefdZdded	edz  ddfd
Z	e
deeej                        fd       Zy)r   ztAn object to represent the JWE Compact Serialization. It is usually returned by
    ``decrypt_compact`` method.
    Nr.   	plaintextc                 J    || _         || _        d | _        i | _        i | _        y r    )r.   rB   	recipientbytes_segmentsbase64_segments)r%   r.   rB   s      r&   r'   zCompactEncryption.__init__D   s'    ""260213r(   r)   c                     | j                   S )z,Returns the protected header values in dict.)r.   r%   s    r&   r0   zCompactEncryption.headersM   s    ~~r(   keyr   c                 f    t        | d|      }|r| j                  j                  |       || _        y)zAdd a recipient to the JWE Compact Serialization. Please add a key that
        comply with the given "alg" value.

        :param key: an instance of a key, e.g. (OctKey, RSAKey, ECKey, and etc)
        :param header: extra header in dict
        N)r   r.   r-   rD   )r%   rI   r   rD   s       r&   attach_recipientz"CompactEncryption.attach_recipientQ   s.     dD#.	NN!!&)"r(   c                 8    | j                   | j                   gS g S r    )rD   rH   s    r&   
recipientszCompactEncryption.recipients]   s    >>%NN##	r(   r    )r9   r:   r;   __doc__r   bytesr'   r0   r
   rK   propertylistr   r<   r?   rM   r@   r(   r&   r   r   ?   sq    4& 4UT\ 4 
#C 
#$ 
#$ 
# D155!12  r(   r   c            
       0   e Zd ZU ej                  e   ed<   eed<   ej                  e	   ed<   ej                  e   ed<   ej                  e	   ed<   e
eej                        ed<   	 	 	 ddede	dz  dedz  de	dz  fdZedd	edz  d
edz  ddfd       Zy)r   	flattenedr.   rB   r,   aadrM   Nc                 f    || _         || _        || _        || _        g | _        i | _        i | _        y r    )r.   rB   r,   rT   rM   rE   rF   )r%   r.   rB   r,   rT   s        r&   r'   zBaseJSONEncryption.__init__r   s8     #"&0213r(   r   rI   r)   c                      y)a  Add a recipient to the JWE JSON Serialization. Please add a key that
        comply with the "alg" to this recipient.

        :param header: recipient's own (unprotected) header
        :param key: an instance of a key, e.g. (OctKey, RSAKey, ECKey, and etc)
        Nr@   )r%   r   rI   s      r&   add_recipientz BaseJSONEncryption.add_recipient   s    r(   )NNNr8   )r9   r:   r;   r<   ClassVarbool__annotations__r   OptionalrO   rQ   r   r?   r'   r   r
   rW   r@   r(   r&   r   r   d   s    zz$zz%  F##	
E	Yquu%&&
 #'%) 44 4<4 d]	4
 T\4 FTM sTz UY  r(   r   )	metaclassc                   6    e Zd ZdZdZddedz  dedz  ddfdZy)	r   a  An object to represent the JWE General JSON Serialization. It is used by
    ``encrypt_json``, and it is usually returned by ``decrypt_json`` method.

    To construct an object of ``GeneralJSONEncryption``:

    .. code-block:: python

        protected = {"enc": "A128CBC-HS256"}
        plaintext = b"hello world"
        obj = GeneralJSONEncryption(protected, plaintext)
        # then add each recipient
        obj.add_recipient({"alg": "A128KW"})
    FNr   rI   r)   c                     t        | ||      }|r'|j                  r|j                  |j                         | j                  j	                  |       y r    )r   r5   r7   rM   appendr%   r   rI   rD   s       r&   rW   z#GeneralJSONEncryption.add_recipient   s=    dFC0	377cgg&y)r(   r8   r9   r:   r;   rN   rS   r   r
   rW   r@   r(   r&   r   r      s1     I*FTM *sTz *UY *r(   r   c                   6    e Zd ZdZdZddedz  dedz  ddfdZy)	r   a  An object to represent the JWE Flattened JSON Serialization. It is used by
    ``encrypt_json``, and it is usually returned by ``decrypt_json`` method.

    To construct an object of ``FlattenedJSONEncryption``:

    .. code-block:: python

        protected = {"enc": "A128CBC-HS256"}
        plaintext = b"hello world"
        obj = FlattenedJSONEncryption(protected, plaintext)
        # then add each recipient
        obj.add_recipient({"alg": "A128KW"})
    TNr   rI   r)   c                     t        | ||      }|r'|j                  r|j                  |j                         |g| _        y r    )r   r5   r7   rM   r`   s       r&   rW   z%FlattenedJSONEncryption.add_recipient   s4    dFC0	377cgg&$+r(   r8   ra   r@   r(   r&   r   r      s1     I&FTM &sTz &UY &r(   r   c                      e Zd ZU eed<   eed<   dZeed<   dZej                  d   ed<   dZ
ej                  d   ed<   eed	<   eed
<   defdZdefdZdedefdZedededededeeef   f
d       Zededededededefd       Zy)r   namedescriptionFrecommendedJWEalgorithm_typeencalgorithm_locationiv_sizecek_sizer)   c                 F    t        j                  | j                  dz        S N   )secretstoken_bytesrm   rH   s    r&   generate_cekzJWEEncModel.generate_cek   s    ""4==A#566r(   c                 F    t        j                  | j                  dz        S ro   )rq   rr   rl   rH   s    r&   generate_ivzJWEEncModel.generate_iv   s    ""4<<1#455r(   ivc                 R    t        |      dz  | j                  k7  rt        d      |S )Nrp   zInvalid 'iv' size)lenrl   
ValueError)r%   rv   s     r&   check_ivzJWEEncModel.check_iv   s'    r7Q;$,,&011	r(   rB   cekrT   c                      y r    r@   )r%   rB   r{   rv   rT   s        r&   encryptzJWEEncModel.encrypt       r(   
ciphertexttagc                      y r    r@   )r%   r   r   r{   rv   rT   s         r&   decryptzJWEEncModel.decrypt   r~   r(   N)r9   r:   r;   r>   rZ   rg   rY   ri   r<   Literalrk   intrO   rs   ru   rz   r   tupler}   r   r@   r(   r&   r   r      s    
IK',NAIIe$,+0		%(0LM7e 76U 65 U 
  U  E eTY[`T`Na   % e % U QV [`  r(   r   c                       e Zd ZU eed<   eed<   dZeed<   dZej                  d   ed<   dZ
ej                  d   ed<   ed	ed
efd       Zed	ed
efd       Zy)r   re   rf   Trg   rh   ri   ziprk   sr)   c                      y r    r@   r%   r   s     r&   compresszJWEZipModel.compress   r~   r(   c                      y r    r@   r   s     r&   
decompresszJWEZipModel.decompress   r~   r(   N)r9   r:   r;   r>   rZ   rg   rY   ri   r<   r   rk   r   rO   r   r   r@   r(   r&   r   r      s    
IK',NAIIe$,+0		%(0% E   E e  r(   r   c                      e Zd ZU eed<   eed<   dZeed<   dZedz  ed<   e	e   ed<   dZ
edz  ed<   d	Zej                  d	   ed
<   dZej                  d   ed<   i Zeed<   edefd       ZdeddfdZdeej,                     ddfdZy)r   re   rf   Frg   Nkey_size	key_typessecurity_warningrh   ri   algrk   more_header_registryr)   c                     | j                   d u S r    )r   rH   s    r&   direct_modezKeyManagement.direct_mode   s    }}$$r(   rI   c                 H    |j                   | j                  vr
t               y r    )key_typer   r   )r%   rI   s     r&   check_key_typezKeyManagement.check_key_type   s     <<t~~-%'' .r(   rD   c                     t               r    NotImplementedErrorr%   rD   s     r&   prepare_recipient_headerz&KeyManagement.prepare_recipient_header       !##r(   )r9   r:   r;   r>   rZ   rg   rY   r   r   rQ   r   ri   r<   r   rk   r   r   rP   r   r
   r   r   r?   r   r@   r(   r&   r   r      s    
IKHcDjCy#'cDj'',NAIIe$,+0		%(0/1,1%T % %(# ($ ($)AEE2B $t $r(   r   c                   6    e Zd ZdgZededee   defd       Z	y)r   octsizerD   r)   c                      y r    r@   )r%   r   rD   s      r&   compute_cekzJWEDirectEncryption.compute_cek  r~   r(   N)
r9   r:   r;   r   r   r   r   r   rO   r   r@   r(   r&   r   r     s5    I 	&0A e  r(   r   c                       e Zd Zedefd       Zededee	j                     defd       Zedee	j                     defd       Zy)r   r)   c                      yNFr@   rH   s    r&   r   zJWEKeyEncryption.direct_mode
      r(   r{   rD   c                      y r    r@   r%   r{   rD   s      r&   encrypt_cekzJWEKeyEncryption.encrypt_cek  r~   r(   c                      y r    r@   r   s     r&   decrypt_cekzJWEKeyEncryption.decrypt_cek  r~   r(   N)r9   r:   r;   rP   rY   r   r   rO   r   r<   r?   r   r   r@   r(   r&   r   r   	  sx    T   u 1551A e   Yquu%5 %  r(   r   c                       e Zd ZU eed<   dgZedefd       Zde	ddfdZ
ede	d	e	de	fd
       Zede	d	e	de	fd       Zede	dee   de	fd       Zedee   de	fd       Zy)r   r   r   r)   c                      yr   r@   rH   s    r&   r   zJWEKeyWrapping.direct_mode  r   r(   op_keyNc                 l    t        |      dz  | j                  k7  rt        d| j                   d      y )Nrp   zA key of size z bits MUST be used)rx   r   r	   )r%   r   s     r&   check_op_keyzJWEKeyWrapping.check_op_key  s4    v;?dmm+'.GY(Z[[ ,r(   r{   rI   c                      y r    r@   r%   r{   rI   s      r&   wrap_cekzJWEKeyWrapping.wrap_cek#  r~   r(   ekc                      y r    r@   r%   r   rI   s      r&   
unwrap_cekzJWEKeyWrapping.unwrap_cek'  r~   r(   rD   c                      y r    r@   r   s      r&   r   zJWEKeyWrapping.encrypt_cek+  r~   r(   c                      y r    r@   r   s     r&   r   zJWEKeyWrapping.decrypt_cek/  r~   r(   )r9   r:   r;   r   rZ   r   rP   rY   r   rO   r   r   r   r   r   r   r   r   r@   r(   r&   r   r     s    MIT  \5 \T \ E  %   U  5   u 61B u   Yv%6 5  r(   r   c                   
   e Zd ZU ddgZdZeed<   ej                  e	   ed<   de
e   ddfd	Zed
ede
e   defd       Zed
ede
e   defd       ZdededefdZdededefdZd
ede
e   dedefdZd
ede
e   dedefdZy)r   ECOKPF	tag_awarekey_wrappingrD   r)   Nc                     |j                   }|J | j                  |       |j                  $|j                  |j                  d      }||_        |j                  d|j                  j                  d             y )NT)privateepkF)r   r   r$   generate_key
curve_namer4   as_dict)r%   rD   r   r$   s       r&   prepare_ephemeral_keyz%JWEKeyAgreement.prepare_ephemeral_key9  s{    !//(((M*""*)66}7O7OY]6^M&3I#UI$;$;$C$CE$C$RSr(   rj   c                      y r    r@   r%   rj   rD   s      r&   encrypt_agreed_upon_keyz'JWEKeyAgreement.encrypt_agreed_upon_keyB  r~   r(   c                      y r    r@   r   s      r&   decrypt_agreed_upon_keyz'JWEKeyAgreement.decrypt_agreed_upon_keyF  r~   r(   r{   rI   c                 V    | j                   J | j                   j                  ||      S r    )r   r   r   s      r&   wrap_cek_with_aukz!JWEKeyAgreement.wrap_cek_with_aukJ  s-      ,,,  ))#s33r(   r   c                 V    | j                   J | j                   j                  ||      S r    )r   r   r   s      r&   unwrap_cek_with_aukz#JWEKeyAgreement.unwrap_cek_with_aukN  s-      ,,,  ++B44r(   r   c                     t               r    r   r%   rj   rD   r   s       r&    encrypt_agreed_upon_key_with_tagz0JWEKeyAgreement.encrypt_agreed_upon_key_with_tagR  r   r(   c                     t               r    r   r   s       r&    decrypt_agreed_upon_key_with_tagz0JWEKeyAgreement.decrypt_agreed_upon_key_with_tagU  r   r(   )r9   r:   r;   r   r   rY   rZ   r<   r[   r   r   r   r   r   r   rO   r   r   r   r   r   r   r@   r(   r&   r   r   4  s#   uIIt**^,,Ty/? TD T ; 9UCS X]   ; 9UCS X]  4U 4 45 45e 5% 5E 5$K $IV[L\ $ch $mr $$K $IV[L\ $ch $mr $r(   r   )"typingr<   rq   abcr   r   registryr   r   errorsr   r	   _keysr
   r   r   __all__TypeVarr   Genericr   r   r   r   r   r   r   r   r   r   r   r   r=   r   r@   r(   r&   <module>r      s      ' 1 ? & &  !))I
!$		'" !$H" "J$7 $N*. *0&0 &0G <G  $ $0-7 } ]g :"$mw "$J gg&I\\]r(   