
    \AhX                     ,   S SK r S SKrS SKrS SKJr  S SKJr  SSKJr  SSK	J
r
  SSKJrJrJr  SSKJrJr  SS	KJrJrJr  \R,                  \R.                  SSS4S
 jrS rS rS rS rS rS rS rS r S r!S r"S r#S r$S r%S r&S r'S r(S r)S r*g)    N)Mappingpack   )jwk)get_random_bytes)
ALGORITHMSJWE_SIZE_LIMITZIPS)JWEErrorJWEParseError)base64url_decodebase64url_encodeensure_binaryc                 :   [        U 5      n U[        R                  ;  a  [        SU-  5      eU[        R                  ;  a  [        SU-  5      e[        R
                  " X5      n[        X2XEU5      n[        X@5      n [        XX$X5      u  pp[        XxXU5      nU$ )aK  Encrypts plaintext and returns a JWE compact serialization string.

Args:
    plaintext (bytes): A bytes object to encrypt
    key (str or dict): The key(s) to use for encrypting the content. Can be
        individual JWK or JWK set.
    encryption (str, optional): The content encryption algorithm used to
        perform authenticated encryption on the plaintext to produce the
        ciphertext and the Authentication Tag.  Defaults to A256GCM.
    algorithm (str, optional): The cryptographic algorithm used
        to encrypt or determine the value of the CEK.  Defaults to dir.
    zip (str, optional): The compression algorithm) applied to the
        plaintext before encryption. Defaults to None.
    cty (str, optional): The media type for the secured content.
        See http://www.iana.org/assignments/media-types/media-types.xhtml
    kid (str, optional): Key ID for the provided key

Returns:
    bytes: The string representation of the header, encrypted key,
        initialization vector, ciphertext, and authentication tag.

Raises:
    JWEError: If there is an error signing the token.

Examples:
    >>> from jose import jwe
    >>> jwe.encrypt('Hello, World!', 'asecret128bitkey', algorithm='dir', encryption='A128GCM')
    'eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..McILMB3dYsNJSuhcDzQshA.OfX9H_mcUpHDeRM4IA.CcnTWqaqxNsjT4eCaUABSg'

Algorithm %s not supported.)
r   r	   	SUPPORTEDr   r   	construct_encoded_header	_compress_encrypt_and_auth_jwe_compact_serialize)	plaintextkey
encryption	algorithmzipctykidencoded_headerenc_cekivcipher_textauth_tag
jwe_strings                OC:\Users\julio\Documents\inmobiliaria_backend\env\Lib\site-packages\jose/jwe.pyencryptr'      s    > i(I
,,,4y@AA---4zABB
--
'C$YCcJN#)I):3:\e)v&G'RZ[J    c                 >   [        U 5      [        :  a  [        S[        U 5       S[         S35      e[        U 5      u  p#pEpg US   nUS   n	U[        R
                  ;  a  [        SU-  5      eU	[        R
                  ;  a  [        SU	-  5      e [        R                  " X5      nU[        R                  :X  a  US:H  n
[        U5      nO UR                  U5      nS	n
UnUn [!        XXeX5      nUb  [#        UR%                  S5      U5      nU
(       a  U$ S$ ! [         a    [        S5      ef = f! [         a    [        S
U S35      e[         a    Sn
[        U	5      n Nf = f! [         a    [        SU	 S35      e[         a  n[        U5      eSnAff = f)a  Decrypts a JWE compact serialized string and returns the plaintext.

Args:
    jwe_str (str): A JWE to be decrypt.
    key (str or dict): A key to attempt to decrypt the payload with. Can be
        individual JWK or JWK set.

Returns:
    bytes: The plaintext bytes, assuming the authentication tag is valid.

Raises:
    JWEError: If there is an exception verifying the token.

Examples:
    >>> from jose import jwe
    >>> jwe.decrypt(jwe_string, 'asecret128bitkey')
    'Hello, World!'
zJWE string z bytes exceeds  bytesalgencr   z!alg and enc headers are required!r(   Talg  is not implementedFenc Nr   )lenr
   r   _jwe_compact_deserializer	   r   KeyErrorr   r   r   DIR_get_key_bytes_from_key
unwrap_keyNotImplementedError	Exception_get_random_cek_bytes_for_enc_decrypt_and_auth_decompressget)jwe_strr   headerr    encrypted_keyr"   r#   r$   r+   r,   	cek_valid	cek_bytesprotected_headeraad
plain_textes                   r&   decryptrE   <   s   . 7|n$S\N/.AQQWXYYG_`gGhDFM{A UmUmj***83>??j***83>?? + --
!C& jnn "S(	 ,C0		;}5I I* & C&y{V
  E!2J?
":,,y  A?@@AT # 	<T#&9:;; 	; I 6c:I	;L  8cU"5677 qks7   AD D5 &E* D25/E'&E'*"FFFc                 "    [        U 5      S   nU$ )a  Returns the decoded headers without verification of any kind.

Args:
    jwe_str (str): A compact serialized JWE to decode the headers from.

Returns:
    dict: The dict representation of the JWE headers.

Raises:
    JWEError: If there is an exception decoding the JWE.
r   )r1   )r<   r=   s     r&   get_unverified_headerrG      s     &g.q1FMr(   c                 "   U[         R                  ;   a  [        X5      u  pgn[        X#XGU5      n	O<U[         R                  ;   a  [
        R                  " X5      nUn	O[        SU S35      eUR                  X#XE5      n
XY:w  a  [        S5      eU
$ )a  
Decrypt and verify the data

Args:
    cek_bytes (bytes): cek to derive encryption and possible auth key to
        verify the auth tag
    cipher_text (bytes): Encrypted data
    iv (bytes): Initialization vector (iv) used to encrypt data
    aad (bytes): Additional Authenticated Data used to verify the data
    auth_tag (bytes): Authentication ntag to verify the data

Returns:
    (bytes): Decrypted data
r/    is not implemented!zInvalid JWE Auth Tag)
r	   HMAC_AUTH_TAG3_get_encryption_key_mac_key_and_key_length_from_cek	_auth_tagGCMr   r   r6   rE   r   )r@   r,   r#   r"   rB   r$   encryption_keymac_keykey_lenauth_tag_checkr   s              r&   r9   r9      s    , j&&&+^_h+n(";C'J	
	y6!!D-A"BCC&&{FI!-..r(   c                     [        U 5      S-  nU SU n[        X5      nX* S  nUR                  S5      u  pg[        R                  " XV5      nXU4$ )N   r   -)r0   _get_hmac_keysplitr   r   )	r@   r,   derived_key_lenmac_key_bytesrO   encryption_key_bytesencryption_alg_rN   s	            r&   rK   rK      s_    )n)Oa0MC/G$%5%67		#N]]#7HNO33r(   c                 6   [        U 5      n  U R                  SS5      u  pp4n[        U5      n [        R                  " U5      n[        U[        5      (       d  [	        S5      e [        U5      n	 [        U5      n
 [        U5      n [        U5      nXqXX4$ ! [         a    [	        S5      e[
        [        R                  4 a    [	        S5      ef = f! [         a  n[	        SU 35      eSnAff = f! [
        [        R                  4 a    [	        S5      ef = f! [
        [        R                  4 a    [	        S	5      ef = f! [
        [        R                  4 a    [	        S
5      ef = f! [
        [        R                  4 a    [	        S5      ef = f)z
Deserialize and verify the header and segments are appropriate.

Args:
    jwe_bytes (bytes): The compact serialized JWE
Returns:
    (dict, bytes, bytes, bytes, bytes, bytes)
   .   zNot enough segmentszInvalid headerzInvalid header string: Nz,Invalid header string: must be a json objectzInvalid encrypted keyz
Invalid IVzInvalid cyphertextzInvalid auth tag)r   rV   r   
ValueErrorr   	TypeErrorbinasciiErrorjsonloads
isinstancer   )	jwe_bytesheader_segmentencrypted_key_segment
iv_segmentcipher_text_segmentauth_tag_segmentheader_datar=   rD   r>   r"   
ciphertextr$   s                r&   r1   r1     s    i(I.clcrcr!d
`zP` '~6.;K( fg&&JKK5()>?*j)2%&9:
0#$45 =jJJe  3122x~~& .,--.,  ;5aS9::; x~~& 53445
 x~~& *L))*
 x~~& 20112
 x~~& 0.//0sR   !B C &C7 2D  >E	 
E2 :C
C4!C//C47&D &E	&E/2&Fc                     XS.nU(       a  X%S'   U(       a  X5S'   U(       a  XES'   [         R                  " USSS9R                  S5      n[        U5      $ )	a]  
Generate an appropriate JOSE header based on the values provided
Args:
    alg (str): Key wrap/negotiation algorithm
    enc (str): Encryption algorithm
    zip (str): Compression method
    cty (str): Content type of the encrypted data
    kid (str): ID for the key used for the operation

Returns:
    bytes: JSON object of header based on input
)r+   r,   r   r   r   ),:T)
separators	sort_keyszutf-8)rc   dumpsencoder   )r+   r,   r   r   r   r=   json_headers          r&   r   r   Q  s\     %F
u
u
u** fWo	 
 K((r(   c                     [        SU 5      $ )Nz!Qr   )int_vals    r&   _big_endianrx   m  s    gr(   c                     [        X!U 5      u  pgU[        R                  ;   a0  [        Xb5      u  pn
UR                  XE5      u  pn[        XXYU
5      nONU[        R                  ;   a+  [        R                  " Xb5      nUR                  XE5      u  pnO[        SU S35      eX{X4$ ! [         a    [        SU S35      ef = f)a  
Generate a content encryption key (cek) and initialization
vector (iv) based on enc and alg, compress the plaintext based on zip,
encrypt the compressed plaintext using the cek and iv based on enc

Args:
    key (Key): The key provided for encryption
    alg (str): The algorithm use for key wrap/negotiation
    enc (str): The encryption algorithm with which to encrypt the plaintext
    zip (str): The compression algorithm with which to compress the plaintext
    plaintext (bytes): The data to encrypt
    aad (str): Additional authentication data utilized for generating an
                auth tag

Returns:
      (bytes, bytes, bytes, bytes): A tuple of the following data
                             (key wrapped cek, iv, cipher text, auth tag)
r-   r.   r/   rI   )_get_cekr6   r   r	   rJ   rK   r'   rL   rM   r   r   )r   r+   r,   r   r   rB   r@   kw_cekrN   rO   rP   r"   rm   tagr$   s                  r&   r   r   q  s    &8$Ss3	 j&&&+^_h+n(,44YDZS7C	
	y6#1#9#9)#I !D-A"BCCz++  8cU"56778s   B& &C c                 X    U R                  S5      u  p#[        R                  " X5      nU$ )z
Get an HMACKey for the provided encryption algorithm and key bytes

Args:
    enc (str): Encryption algorithm
    mac_key_bytes (bytes): vytes for the HMAC key

Returns:
     (HMACKey): The key to perform HMAC actions
rT   )rV   r   r   )r,   rX   r[   hash_algrO   s        r&   rU   rU     s&     ))C.KAmmM4GNr(   c                     U [         R                  ;  a  [        SU  S35      eU c  UnU$ U [         R                  :X  a  [        R
                  " U5      nU$ [        SU  S35      e)z
Compress the plaintext based on the algorithm supplied

Args:
    zip (str): Compression Algorithm
    plaintext (bytes): plaintext to compress

Returns:
    (bytes): Compressed plaintext
ZIP  is not supported!rI   )r   r   r6   DEFzlibcompress)r   r   
compresseds      r&   r   r     sq     $.. !D-?"@AA
{

 	 
]]9-
  "D-A"BCCr(   c                 <   U [         R                  ;  a  [        SU  S35      eU c  UnU$ U [         R                  :X  aP  [        R
                  " 5       nUR                  U[        S9nUR                  (       a  [        S[         S35      e U$ [        SU  S35      e)z
Decompress the plaintext based on the algorithm supplied

Args:
    zip (str): Compression Algorithm
    plaintext (bytes): plaintext to decompress

Returns:
    (bytes): Compressed plaintext
r   r   )
max_lengthz Decompressed JWE string exceeds r*   rI   )
r   r   r6   r   r   decompressobj
decompressr
   unconsumed_tailr   )r   r   decompresseddecompressors       r&   r:   r:     s     $.. !D-?"@AA
{!  
 ))+#..zn.U''=n=MVTUU (  "D-A"BCCr(   c                 j    U[         R                  :X  a  [        U5      u  p4X44$ [        X5      u  p4X44$ )z
Get the content encryption key

Args:
    enc (str): Encryption algorithm
    alg (str): kwy wrap/negotiation algorithm
    key (Key): Key provided to encryption method

Return:
    (bytes, bytes): Tuple of (cek bytes and wrapped cek)
)r	   r3   _get_direct_key_wrap_cek_get_key_wrap_cek)r,   r+   r   cekwrapped_ceks        r&   rz   rz     s@     jnn3C8  -S6r(   c                     U R                  5       nUS   S:X  a  [        U 5      nSnX#4$ [        SR                  US   5      5      e)z
Get the cek and wrapped cek from the encryption key direct

Args:
    key (Key): Key provided to encryption method

Return:
    (Key, bytes): Tuple of (cek Key object and wrapped cek)
ktyoctr(   zJWK type {} not supported!)to_dictr4   r6   format)r   jwk_datar@   r   s       r&   r   r     sR     {{}H%+C0	 !! "">"E"Ehuo"VWWr(   c                 F    U R                  5       nUS   n[        U5      nU$ )z
Get the raw key bytes from a Key object

Args:
    key (Key): Key from which to extract the raw key bytes
Returns:
    (bytes) key data
k)r   r   )r   r   encoded_keyr@   s       r&   r4   r4      s(     {{}H3-K -Ir(   c                 @    [        U 5      nUR                  U5      nX#4$ )z_get_rsa_key_wrap_cek
Get the content encryption key for RSA key wrap

Args:
    enc (str): Encryption algorithm
    key (Key): Key provided to encryption method

Returns:
    (Key, bytes): Tuple of (cek Key object and wrapped cek)
)r8   wrap_key)r,   r   r@   r   s       r&   r   r     s%     .c2I,,y)K!!r(   c                 D   U [         R                  :X  a  SnOzU [         R                  :X  a  SnOcU [         R                  [         R                  4;   a  SnO<U [         R
                  :X  a  SnO%U [         R                  :X  a  SnO[        U  S35      e[        US-  5      nU$ )z
Get the random cek bytes based on the encryption algorithm

Args:
    enc (str): Encryption algorithm

Returns:
    (bytes) random bytes for cek key
         i  i   z not supported   )	r	   A128GCMA192GCMA128CBC_HS256A256GCMA192CBC_HS384A256CBC_HS512r6   r   )r,   num_bitsr@   s      r&   r8   r8     s     j   	
""	"	)):+=+=>	>	
((	(	
((	(!SE"899 Q/Ir(   c                 t    [        [        U5      S-  5      nX!-   U -   U-   nUR                  U5      nUSU nU$ )a2  
Get ann auth tag from the provided data

Args:
    ciphertext (bytes): Encrypted value
    iv (bytes): Initialization vector
    aad (bytes): Additional Authenticated Data
    mac_key (bytes): Key to use in generating the MAC
    tag_length (int): How log the tag should be

Returns:
    (bytes) Auth tag
r   r   )rx   r0   sign)	rm   r"   rB   rO   
tag_lengthalauth_tag_input	signaturer$   s	            r&   rL   rL   9  sF     
SX\	"BX
*R/N^,I:&HOr(   c                     [        U5      n[        U5      n[        U5      n[        U5      n[        U5      nU S-   U-   S-   U-   S-   U-   S-   U-   $ )aQ  
Generate a compact serialized JWE

Args:
    encoded_header (bytes): Base64 URL Encoded JWE header JSON
    encrypted_cek (bytes): Encrypted content encryption key (cek)
    iv (bytes): Initialization vector (IV)
    cipher_text (bytes): Cipher text
    auth_tag (bytes): JWE Auth Tag

Returns:
    (str): JWE compact serialized string
r]   )r   r   )	r    encrypted_cekr"   r#   r$   encoded_encrypted_cek
encoded_ivencoded_cipher_textencoded_auth_tags	            r&   r   r   N  s      ,K,];!"%J*;7'1
	
	  	 		
 	 	 	 	
r(   )+ra   rc   r   collections.abcr   structr    r   backendsr   	constantsr	   r
   r   
exceptionsr   r   utilsr   r   r   r   r3   r'   rE   rG   r9   rK   r1   r   rx   r   rU   r   r:   rz   r   r4   r   r8   rL   r    r(   r&   <module>r      s       #   & 7 7 / D D (2'9'9Z^^Y]cgmq +\H-V #L4GKT)8",J ,6("*" 4*r(   