
    kKj'                     v   d dl mZmZmZ d dlmZ ddlmZmZ ddl	m
Z
mZ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 dd	lmZmZmZmZ dd
lmZ ddlmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z& ddl'm(Z(m)Z) g dZ* e        	 	 	 d'de(de+e,z  de#dee,   dz  dedz  de!e"z  e z  dz  de,fdZ-	 	 	 d'de+e,z  de#dee,   dz  dedz  de!e"z  e z  dz  defdZ.e	 	 	 d'dede#dz  dee,   dz  dedz  de!e"z  e z  dz  defd       Z/e	 	 	 d'dede#dz  dee,   dz  dedz  de!e"z  e z  dz  defd       Z/	 	 	 d'deez  de#dz  dee,   dz  dedz  de!e"z  e z  dz  deez  fdZ/	 	 	 d'deez  de#dee,   dz  dedz  de!e"z  e z  dz  deez  fd Z0	 d(d!e1e
e      de#de!e"z  e z  dz  ddfd"Z2	 d)d#e
e   d$e!e"z  e z  d%e3de!e"z  fd&Z4y)*    )castoverloadUnion)
Collection   )GeneralJSONSerializationFlattenedJSONSerialization)	RecipientCompactEncryptionGeneralJSONEncryptionFlattenedJSONEncryption)JWERegistrydefault_registry)perform_encryptperform_decrypt)represent_compactextract_compact)represent_general_jsonrepresent_flattened_jsonextract_general_jsonextract_flattened_json)setup_jwe_algorithms)KeyKeySetECKeyOKPKeyKeyFlexible	guess_key)to_bytes)Headerreject_unprotected_crit_header)r   r	   r   r
   r   r   r   encrypt_compactdecrypt_compactencrypt_jsondecrypt_jsonr   N	protected	plaintext
public_key
algorithmsregistry
sender_keyreturnc                 D   |rt        |      }n|t        }t        | t        |            }t	        |      }t        ||dd      }|j                  d       ||_        |rt        ||d      |_	        ||_
        t        ||       t        |      }	|	j                  d      S )a+  Generate a JWE Compact Serialization. The JWE Compact Serialization represents
    encrypted content as a compact, URL-safe string.  This string is::

        BASE64URL(UTF8(JWE Protected Header)) || '.' ||
        BASE64URL(JWE Encrypted Key) || '.' ||
        BASE64URL(JWE Initialization Vector) || '.' ||
        BASE64URL(JWE Ciphertext) || '.' ||
        BASE64URL(JWE Authentication Tag)

    :param protected: protected header part of the JWE, in dict
    :param plaintext: the content (message) to be encrypted
    :param public_key: a public key used to encrypt the CEK
    :param algorithms: a collection (list, tuple, or set) of allowed algorithms
    :param registry: a JWERegistry to use
    :param sender_key: only required when using ECDH-1PU
    :return: JWE Compact Serialization in bytes
    r)   Tencusezutf-8)r   r   r   r   r
   r   	check_userecipient_key_guess_sender_keyr+   	recipientr   r   decode)
r&   r'   r(   r)   r*   r+   objr5   keyouts
             \/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/joserfc/jwe.pyr"   r"   3   s    4 *5		#
Ix	':
;C )#I
J	4U
;CMM%!I0JM	CMC"
C
 C::g    valueprivate_keyc                    |rt        |      }n|t        }t        t        |       |      }|j                  }|J t        ||d      }|j                  d       ||_        |rt        ||      |_	        t        ||       |S )a  Extract and validate the JWE Compact Serialization (in string, or bytes)
    with the given key. An JWE Compact Serialization looks like:

    .. code-block:: text
        :caption: line breaks for display purposes only

        OKOawDo13gRp2ojaHV7LFpZcgV7T6DVZKTyKOMTYUmKoTCVJRgckCL9kiMT03JGe
        ipsEdY3mx_etLbbWSrFr05kLzcSr4qKAq7YN7e9jwQRb23nfa6c9d-StnImGyFDb
        Sv04uVuxIp5Zms1gNxKKK2Da14B8S4rzVRltdYwam_lDp5XnZAYpQdb76FdIKLaV
        mqgfwX7XWRxv2322i-vDxRfqNzo_tETKzpVLzfiwQyeyPGLBIO56YJ7eObdv0je8
        1860ppamavo35UgoRdbYaBcoh9QcfylQr66oc6vFWXRcZ_ZT2LawVCWTIy3brGPi
        6UklfCpIMfIjf7iGdXKHzg

    :param value: a string (or bytes) of the JWE Compact Serialization
    :param private_key: a flexible private key to decrypt the serialization
    :param algorithms: a collection (list, tuple, or set) of allowed algorithms
    :param registry: a JWERegistry to use
    :param sender_key: only required when using ECDH-1PU
    :return: object of the ``CompactEncryption``
    r.   r/   r0   )r   r   r   r   r5   r   r2   r3   r4   r+   r   )r<   r=   r)   r*   r+   r7   r5   r8   s           r:   r#   r#   _   s    6 *5		#
(5/8
4CI   
K
6CMM%!I0JG	C"Jr;   r7   c                      y N r7   r(   r)   r*   r+   s        r:   r$   r$      s      #r;   c                      y r@   rA   rB   s        r:   r$   r$      s     "%r;   c                    |rt        |      }n|t        }t        | j                         | j                  D ]Z  }|r|j
                  st        ||d      |_        |j                  r0|J t        ||dd      }|j                  d       ||_        \ t        | |       t        | t              rt        |       S t        |       S )a  Generate a JWE JSON Serialization (in dict). The JWE JSON Serialization
    represents encrypted content as a JSON object. This representation is neither
    optimized for compactness nor URL safe.

    When calling this method, developers MUST construct an instance of a
    ``GeneralJSONEncryption`` or ``FlattenedJSONEncryption`` object. Here
    is an example::

        from joserfc.jwe import GeneralJSONEncryption

        protected = {"enc": "A128CBC-HS256"}
        plaintext = b"hello world"
        header = {"jku": "https://server.example.com/keys.jwks"}  # optional shared header
        obj = GeneralJSONEncryption(protected, plaintext, header)
        # add the recipients
        obj.add_recipient({"kid": "alice", "alg": "RSA1_5"})  # not configured a key
        bob_key = OctKey.import_key("bob secret")
        obj.add_recipient({"kid": "bob", "alg": "A128KW"}, bob_key)

    :param obj: an instance of ``GeneralJSONEncryption`` or ``FlattenedJSONEncryption``
    :param public_key: a public key used to encrypt the CEK
    :param algorithms: a collection (list, tuple, or set) of allowed algorithms
    :param registry: a JWERegistry to use
    :param sender_key: only required when using ECDH-1PU
    :return: JWE JSON Serialization in dict
    r.   Tr/   r0   )r   r   r!   unprotected
recipientsr+   r4   r3   r   r2   r   
isinstancer   r   r   )r7   r(   r)   r*   r+   r5   r8   s          r:   r$   r$      s    D *5		#"3??3^^	i22#4Y
D#QI &&)))J	4UCCMM% &)I# $ C"#,-%c**#C((r;   datac                 h   |rt        |      }n|t        }t        | j                  d             d| v r?t	        t        t        |       |      }t        |j                  ||       t        ||       |S t        t        t        |       |      }t        |j                  ||       t        ||       |S )a  Decrypt the JWE JSON Serialization (in dict) to a
    ``GeneralJSONEncryption`` or ``FlattenedJSONEncryption`` object.

    :param data: JWE JSON Serialization in dict
    :param private_key: a flexible private key to decrypt the CEK
    :param algorithms: a collection (list, tuple, or set) of allowed algorithms
    :param registry: a JWERegistry to use
    :param sender_key: only required when using ECDH-1PU
    :return: an instance of ``GeneralJSONEncryption`` or ``FlattenedJSONEncryption``
    r.   rE   rF   )r   r   r!   getr   r   r   _attach_recipient_keysrF   r   r   r	   )rH   r=   r)   r*   r+   general_objflattened_objs          r:   r%   r%      s    " *5		#"488M#:;t*40H$+OQYZ{55{JOX..t4NPT/UW_`}77jQx0r;   rF   c                     | D ]<  }t        ||d      }|j                  d       ||_        |s,t        ||      |_        > y )Nr/   r0   )r   r2   r3   r4   r+   )rF   r=   r+   r5   r8   s        r:   rK   rK      sB      	YE:e"%	#4Y
#KI   r;   r5   r8   
use_randomc                    t        |t              s|S | j                         }|j                  d      }|r.t	        t
        t        t        f   |j                  |            }|S |rOt	        t
        t        t        f   |j                  |d               }|| j                  d|j                         |S t        d      )NskidalgzInvalid key)rG   r   headersrJ   r   r   r   r   
get_by_kidpick_random_key
add_headerkid
ValueError)r5   r8   rO   rS   rQ   skeys         r:   r4   r4     s     c6"
!G;;vDE%-(#..*>?E%-(#*=*=gen*MN  2K
]
##r;   )NNNr@   )F)5typingr   r   r   collections.abcr   _rfc7516.typesr   r	   _rfc7516.modelsr
   r   r   r   _rfc7516.registryr   r   _rfc7516.messager   r   _rfc7516.compactr   r   _rfc7516.jsonr   r   r   r   jwar   jwkr   r   r   r   r   r   utilr   r*   r    r!   __all__bytesstrr"   r#   r$   r%   listrK   boolr4   rA   r;   r:   <module>rj      s   ( ( &  ? @  & C C  <$   *.#'15))s{) ) 3$&	)
 D ) '$.) 	)^ *.#'15)3;)) 3$&) D 	)
 '$.) )X 
 *.#'15#	#d"# 3$&# D 	#
 '$.# # 
# 
 *.#'15%	 %d"% 3$&% D 	%
 '$.%  % 
% *.#'154)	!8	84)d"4) 3$&4) D 	4)
 '$.4)  ::4)t *.#'15 
"%?
?   3$&  D 	 
 '$.  44 H nrLYs^$L3>LLQTZN]cLcfjLjL	L QV$~$$)FNV$;$IM$
V^$r;   