
    Dj                        d dl mZ d dlZd dlZd dlZd dlmZmZmZm	Z	 d dl
mZ d dlmZ ddlmZ ddlmZ d	d
lmZmZ d	dlmZmZ d Zd Zd Zd Zd Z G d d          ZdS )    )annotationsN)AnyMappingSequencecast)
InvalidTag)AESGCM   )cbor)sha256   )Ctap2Info)PinProtocol_PinUvc                    t          j        t           j                   }|                    |           |                                z   S N)wbits)zlibcompressobj	MAX_WBITScompressflushdataos     K/home/thesage/.hermes/venv/lib/python3.11/site-packages/fido2/ctap2/blob.py	_compressr   ,   s8    ///A::daggii''    c                    t          j        t           j                   }|                    |           |                                z   S r   )r   decompressobjr   
decompressr   r   s     r   _decompressr#   1   s8    $.111A<<		))r   c                2    dt          j        d|           z   S )Ns   blobz<Q)structpack)	orig_sizes    r   _lb_adr(   6   s    V[y1111r   c                    t          |          }t          j        d          }t          |           }|                    |t          |          t          |                    }|||dS )N   )r   r
      )lenosurandomr	   encryptr   r(   )keyr   r'   nonceaesgcm
ciphertexts         r   _lb_packr4   :   sb    D		IJrNNEC[[Fyy8I8IJJJ   r   c                &   	 |d         }|d         }|d         }t          |           }|                    ||t          |                    }||fS # t          t          t
          f$ r t          d          t          $ r t          d          w xY w)Nr   r
   r+   zInvalid entryz	Wrong key)r	   decryptr(   	TypeError
IndexErrorKeyError
ValueErrorr   )r0   entryr3   r1   r'   r2   
compresseds          r   
_lb_unpackr=   H   s    
&1X
a!H	^^E:vi7H7HII
9$$z8, * * *))) & & &%%%&s   AA ?Bc                  `    e Zd ZdZedd            Z	 	 dddZddZddZddZ	d dZ
d!dZdS )"
LargeBlobsaJ  Implementation of the CTAP2.1 Large Blobs API.

    Getting a largeBlobKey for a credential is done via the LargeBlobKey extension.

    :param ctap: An instance of a CTAP2 object.
    :param pin_uv_protocol: An instance of a PinUvAuthProtocol.
    :param pin_uv_token: A valid PIN/UV Auth Token for the current CTAP session.
    infor   returnboolc                :    | j                             d          du S )N
largeBlobsT)optionsget)r@   s    r   is_supportedzLargeBlobs.is_supported`   s    |--55r   Nctapr   pin_uv_protocolPinProtocol | Nonepin_uv_tokenbytes | Nonec                    |                      |j                  st          d          || _        | j        j        j        dz
  | _        |r|rt          ||          nd | _        d S )Nz)Authenticator does not support LargeBlobs@   )rG   r@   r:   rH   max_msg_sizemax_fragment_lengthr   pin_uv)selfrH   rI   rK   s       r   __init__zLargeBlobs.__init__d   sv       ++ 	JHIII	#'9>#>#C  #/F?L111 	r   Sequence[Mapping[int, Any]]c                   d}d}	 | j                             || j                  d         }||z  }t          |          | j        k     rn|| j        z  }P|dd         |dd         }}|t	          |          dd         k    rg S t          t          t          t          t          f                  t          j        |                    S )zsGets the entire contents of the Large Blobs array.

        :return: The CBOR decoded list of Large Blobs.
        r   r   T)rF   r   Ni)rH   large_blobsrP   r,   r   r   r   r   intr   r   decode)rR   offsetbuffragmentr   checks         r   read_blob_arrayzLargeBlobs.read_blob_arrayu   s    
 	/y,,V9Q,RRSTUH8OC8}}t777d..F	/ $3$iSTTeF4LL#&&&IHWS#X./T1B1BCCCr   
blob_arrayNonec                h   t          |t                    st          d          t          j        |          }|t          |          dd         z  }d}t          |          }||k     rt          ||z
  | j                  }||||z            }| j	        rddt          j        d|          z   t          |          z   }| j	        j        j        }| j	        j                            | j	        j        |          }	nd}	d}| j                            |||dk    r|nd||	           ||z  }||k     dS dS )zoWrites the entire Large Blobs array.

        :param blob_array: A list to write to the Authenticator.
        zlarge-blob array must be a listN   r   s"    z<I)setlengthrI   pin_uv_param)
isinstancelistr7   r   encoder   r,   minrP   rQ   r%   r&   protocolVERSIONauthenticatetokenrH   rV   )
rR   r^   r   rY   sizeln_setmsgrI   rd   s
             r   write_blob_arrayzLargeBlobs.write_blob_array   sZ   
 *d++ 	?=>>>{:&&tSbS!!4yytmmTF]D$<==B",-D{ '"k$//0 Tll#  #'+"6">#{3@@ARTWXX#"&I!!%{{tt /) "    bLF3 tmmmmmmr   large_blob_keybytesc                    |                                  D ]X}	 t          ||          \  }}t          |          }t          |          |k    r|c S =# t          t
          j        f$ r Y Uw xY wdS )zGets the Large Blob stored for a single credential.

        :param large_blob_key: The largeBlobKey for the credential, or None.
        :returns: The decrypted and deflated value stored for the credential.
        N)r]   r=   r#   r,   r:   r   error)rR   rr   r;   r<   r'   decompresseds         r   get_blobzLargeBlobs.get_blob   s     ))++ 	 	E(2>5(I(I%
I*:66|$$	11'''' 2
+   ts   6AA+*A+r   c                ,   |du}g }|                                  D ]:}	 t          ||           d}# t          $ r |                    |           Y 7w xY w|#|                    t	          ||                     |r|                     |           dS dS )a   Stores a Large Blob for a single credential.

        Any existing entries for the same credential will be replaced.

        :param large_blob_key: The largeBlobKey for the credential.
        :param data: The data to compress, encrypt and store.
        NT)r]   r=   r:   appendr4   rq   )rR   rr   r   modifiedentriesr;   s         r   put_blobzLargeBlobs.put_blob   s     t#))++ 	& 	&E&>5111 & & &u%%%%%& NN8ND99::: 	+!!'*****	+ 	+s   1AAc                2    |                      |d           dS )zDeletes any Large Blob(s) stored for a single credential.

        :param large_blob_key: The largeBlobKey for the credential.
        N)r|   )rR   rr   s     r   delete_blobzLargeBlobs.delete_blob   s    
 	nd+++++r   )r@   r   rA   rB   )NN)rH   r   rI   rJ   rK   rL   )rA   rT   )r^   rT   rA   r_   )rr   rs   rA   rL   )rr   rs   r   rL   rA   r_   )rr   rs   rA   r_   )__name__
__module____qualname____doc__staticmethodrG   rS   r]   rq   rw   r|   r~    r   r   r?   r?   V   s          6 6 6 \6 /3%)	
 
 
 
 
"D D D D&& & & &P    + + + +0, , , , , ,r   r?   )
__future__r   r-   r%   r   typingr   r   r   r   cryptography.exceptionsr   +cryptography.hazmat.primitives.ciphers.aeadr	    r   utilsr   baser   r   pinr   r   r   r#   r(   r4   r=   r?   r   r   r   <module>r      sW  8 # " " " " " 				   / / / / / / / / / / / / . . . . . . > > > > > >                     $ $ $ $ $ $ $ $( ( (
* * *
2 2 2  & & &G, G, G, G, G, G, G, G, G, G,r   