
    Wwgv              	       v   d dl 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 dlmZ d dlmZ d dlmZ  G d	 d
ej,                        Z G d dej0                        Z G d de j4                        Z ed       G d d             Ze j:                   G d dej<                               Z G d d      Z  G d d      Z! G d de!ejD                        Z# ejH                  d      Z% G d d      Z&eejN                  e(ge!f   Z)deejH                  e)f   d ejN                  d!e(d"ee!   fd#Z*y)$    N)	dataclass)CallableDictIterableListOptionalSetTupleType)genericmisc)SerialisableCredential)PdfPermissions)DeveloperExtension)PdfReadErrorc                       e Zd Zy)PdfKeyNotAvailableErrorN)__name__
__module____qualname__     R/var/www/horilla/myenv/lib/python3.12/site-packages/pyhanko/pdf_utils/crypt/api.pyr   r      s    r   r   c                       e Zd ZdZdZdZdZy)
AuthStatusz?
    Describes the status after an authentication attempt.
    r         N)r   r   r   __doc__FAILEDUSEROWNERr   r   r   r   r      s     FDEr   r   c                       e Zd ZdZdZdZdZy)PdfMacStatusz'
    Status of PDF MAC validation.
    r   r   r   N)r   r   r   r   NOT_APPLICABLE
SUCCESSFULr   r   r   r   r#   r#      s     NJFr   r#   T)frozenc                   l    e Zd ZU dZeed<   	 dZee   ed<   	 e	j                  Ze	ed<   	 dZee   ed<   y)
AuthResultz<
    Describes the result of an authentication attempt.
    statusNpermission_flags
mac_statusmac_failure_reason)r   r   r   r   r   __annotations__r*   r   r   r#   r$   r+   r,   strr   r   r   r(   r(   $   sX      26h~.5
  ,::J: )-,r   r(   c                   l    e Zd ZdZdZdZdZdZdZdZ		 de
j                  fd	Zedd
       ZdedefdZy)SecurityHandlerVersionz
    Indicates the security handler's version.

    The enum constants are named more or less in accordance with the
    cryptographic algorithms they permit.
    r   r            Nreturnc                 p    | j                   }|t        j                         S t        j                  |      S N)valuer   
NullObjectNumberObject)selfvals     r   as_pdf_objectz$SecurityHandlerVersion.as_pdf_objectT   s3    jj$'KG 	
5<5I5I#5N	
r   c                 X    	 t        |      S # t        $ r t         j                  cY S w xY wr6   )r0   
ValueErrorOTHER)clsr7   s     r   from_numberz"SecurityHandlerVersion.from_numberZ   s-    	0)%00 	0)///	0s   
 ))
key_lengthc                     | t         j                  k(  ry| t         j                  k(  ryd|cxk  rdk  s*n | t         j                  k  rt	        j
                  d      |S )Nr2          z#Key length must be between 5 and 16)r0   RC4_40AES256RC4_OR_AES128r   PdfError)r:   rB   s     r   check_key_lengthz'SecurityHandlerVersion.check_key_lengtha   sX    )000+222j&B&.<<<-- EFFr   )r4   r0   )r   r   r   r   rF   RC4_LONGER_KEYSrH   rG   AES_GCMr?   r   	PdfObjectr<   classmethodrA   intrJ   r   r   r   r0   r0   @   se     FOMFGE
w00 
 0 0
3 
3 
r   r0   c                      e Zd ZU dZi Zeeed    f   ed<   i Z	ee
j                  df   ed<   	 	 	 d(deddd	ee   fd
Zd Zeded    fd       Zede
j&                  dd fd       Zedefd       Zdee   fdZedee   fd       Zede
j&                  fd       ZdefdZde
j&                  fdZd)defdZ d*dZ!d)d*dZ"d Z#defdZ$defdZ%e&defd       Z'ede
j&                  d eded   fd!       Z(ede
j&                  ded   fd"       Z)ed#e
j                  d$dfd%       Z*dee+e,e,f      fd&Z-de.e/   fd'Z0y)+SecurityHandlera'  
    Generic PDF security handler interface.

    This class contains relatively little actual functionality, except for
    some common initialisation logic and bookkeeping machinery to register
    security handler implementations.

    :param version:
        Indicates the version of the security handler to use, as described
        in the specification. See :class:`.SecurityHandlerVersion`.
    :param legacy_keylen:
        Key length in bytes (only relevant for legacy encryption handlers).
    :param crypt_filter_config:
        The crypt filter configuration for the security handler, in the
        form of a :class:`.CryptFilterConfiguration` object.

        .. note::
            PyHanko implements legacy security handlers (which, according to
            the standard, aren't crypt filter-aware) using crypt filters
            as well, even though they aren't serialised to the output file.
    :param encrypt_metadata:
        Flag indicating whether document (XMP) metadata is to be encrypted.

        .. warning::
            Currently, PyHanko does not manage metadata streams, so until
            that changes, it is the responsibility of the API user to mark
            metadata streams using the `/Identity` crypt filter as required.

            Nonetheless, the value of this flag is required in key derivation
            computations, so the security handler needs to know about it.
    :param kdf_salt:
        Optional salt value used when deriving additional key material from
        the main file encryption key.

        .. note::
            This is currently only relevant for the ISO/TS 32004 (PDF MAC)
            extension.
    :param compat_entries:
        Write deprecated but technically unnecessary configuration settings for
        compatibility with certain implementations.
    '_SecurityHandler__registered_subclassesCryptFilterBuilder_known_crypt_filtersNversioncrypt_filter_configCryptFilterConfigurationkdf_saltc                     || _         |j                  |        |j                  |      | _        || _        || _        || _        d | _        || _        y r6   )	rU   set_security_handlerrJ   keylenrV   encrypt_metadata_compat_entries_credential	_kdf_salt)r:   rU   legacy_keylenrV   r\   compat_entriesrX   s          r   __init__zSecurityHandler.__init__   sR     006..}=#6  0-=A!r   c                 V    d| j                   vrt        | j                        | _        y y )NrT   )__dict__dictrT   )r@   kwargss     r   __init_subclass__z!SecurityHandler.__init_subclass__   s(     "5'+C,D,D'EC$ 6r   r@   c                 H    | t         j                  | j                         <   | S )z
        Register a security handler class.
        Intended to be used as a decorator on subclasses.

        See :meth:`build` for further information.

        :param cls:
            A subclass of :class:`.SecurityHandler`.
        )rQ   rR   get_namer@   s    r   registerzSecurityHandler.register   s!     CF//?
r   encrypt_dictr4   c           	         | j                  dd      }	 t        j                  |   }|j                  |       S # t        $ r 	 | d   n&# t        $ r t	        j
                  d| d      w xY w	 t        fdt        j                  j                         D              }n)# t        $ r t	        j
                  d| d d      w xY wY w xY w)	a  
        Instantiate an appropriate :class:`.SecurityHandler` from a PDF
        document's encryption dictionary.

        PyHanko will search the registry for a security handler with
        a name matching the ``/Filter`` entry. Failing that, a security
        handler implementing the protocol designated by the
        ``/SubFilter`` entry (see :meth:`support_generic_subfilters`) will be
        chosen.

        Once an appropriate :class:`.SecurityHandler` subclass has been
        selected, pyHanko will invoke the subclass's
        :meth:`instantiate_from_pdf_object` method with the original encryption
        dictionary as its argument.

        :param encrypt_dict:
            A PDF encryption dictionary.
        :return:
        z/Filterz	/Standardz
/SubFilterz#There is no security handler named zL, and the encryption dictionary does not contain a generic /SubFilter entry.c              3   F   K   | ]  }|j                         v r|  y wr6   )support_generic_subfilters).0h	subfilters     r   	<genexpr>z(SecurityHandler.build.<locals>.<genexpr>   s*       A$@$@$BB s   !zE, and none of the available handlers support the declared /SubFilter .)
getrQ   rR   KeyErrorr   r   nextvaluesStopIterationinstantiate_from_pdf_object)rl   handler_namer@   rr   s      @r   buildzSecurityHandler.build   s    * $''	;?	!99,GC4 ..|<<3  	(6	 ''9, H( )  ,DDKKM 
 ! ''9, H""+A/  	s?   9 	CA	C	#A,,C00B! C!&CCCc                     t         )z
        Retrieves the name of this security handler.

        :return:
            The name of this security handler.
        NotImplementedErrorrj   s    r   ri   zSecurityHandler.get_name   
     "!r   c                 P    t        | j                  t              r| j                  S y)a   
        Extract a serialisable credential for later use, if the security handler
        supports it. It should allow the security handler to be unlocked
        with the same access level as the current one.

        :return:
            A serialisable credential, or ``None``.
        N)
isinstancer^   r   r:   s    r   extract_credentialz"SecurityHandler.extract_credential  s&     d&&(>?### r   c                     t               S )aP  
        Indicates the generic ``/SubFilter`` values that this security handler
        supports.

        :return:
            A set of generic protocols (indicated in the ``/SubFilter`` entry
            of an encryption dictionary) that this :class:`.SecurityHandler`
            class implements. Defaults to the empty set.
        )setrj   s    r   ro   z*SecurityHandler.support_generic_subfilters  s     ur   c                     t         )z
        Instantiate an object of this class using a PDF encryption dictionary
        as input.

        :param encrypt_dict:
            A PDF encryption dictionary.
        :return:
        r~   )r@   rl   s     r   rz   z+SecurityHandler.instantiate_from_pdf_object#  s
     "!r   c                 D    	 | j                          y# t        $ r Y yw xY w)a.  
        Return ``True`` if the security handler has been successfully
        authenticated against for document encryption purposes.

        The default implementation just attempts to call
        :meth:`get_file_encryption_key` and returns ``True`` if that doesn't
        raise an error.
        TF)get_file_encryption_keyr   r   s    r   is_authenticatedz SecurityHandler.is_authenticated1  s(    	((*& 		s    	c                     t         )z
        Serialise this security handler to a PDF encryption dictionary.

        :return:
            A PDF encryption dictionary.
        r~   r   s    r   r<   zSecurityHandler.as_pdf_object@  
     "!r   c                     t         )a  
        Authenticate a credential holder with this security handler.

        :param credential:
            A credential.
            The type of the credential is left up to the subclasses.
        :param id1:
            The first part of the document ID of the document being accessed.
        :return:
            An :class:`AuthResult` object indicating the level of access
            obtained.
        r~   )r:   
credentialid1s      r   authenticatezSecurityHandler.authenticateI  s
     "!r   CryptFilterc                 6    | j                   j                         S )z
        :return:
            The crypt filter responsible for decrypting strings
            for this security handler.
        )rV   get_for_stringr   s    r   get_string_filterz!SecurityHandler.get_string_filterX  s     ''6688r   c                 X    || j                   j                         S | j                   |   S )a  
        :param name:
            Optionally specify a crypt filter by name.
        :return:
            The default crypt filter responsible for decrypting streams
            for this security handler, or the crypt filter named ``name``,
            if not ``None``.
        )rV   get_for_stream)r:   names     r   get_stream_filterz!SecurityHandler.get_stream_filter`  s/     <++::<<''--r   c                 6    | j                   j                         S )z
        :return:
            The crypt filter responsible for decrypting embedded files
            for this security handler.
        )rV   get_for_embedded_filer   s    r   get_embedded_file_filterz(SecurityHandler.get_embedded_file_filterm  s     ''==??r   c                     t         )a  
        Retrieve the global file encryption key (used for streams and/or
        strings). If there is no such thing, or the key is not available,
        an error should be raised.

        :raise PdfKeyNotAvailableError: when the key is not available
        r~   r   s    r   r   z'SecurityHandler.get_file_encryption_keyu  r   r   c                 8    | j                   }|t        d      |S )z
        Get KDF salt value, or raise an error if there is none.

        .. note::
            This is currently only relevant for the ISO/TS 32004 (PDF MAC)
            extension.

        :return:
            The KDF salt value.
        zNo KDF salt available)r_   r   )r:   salts     r   get_kdf_saltzSecurityHandler.get_kdf_salt  s#     ~~<677r   c                     | j                   duS )zg
        Boolean indicating whether this security handler has PDF MAC
        support enabled.
        N)r_   r   s    r   pdf_mac_enabledzSecurityHandler.pdf_mac_enabled  s     ~~T))r   cfdictacts_as_defaultc                 0    t        | j                  ||      S )aD  
        Interpret a crypt filter dictionary for this type of security handler.

        :param cfdict:
            A crypt filter dictionary.
        :param acts_as_default:
            Indicates whether this filter is intended to be used in
            ``/StrF`` or ``/StmF``.
        :return:
            An appropriate :class:`.CryptFilter` object, or ``None``
            if the crypt filter uses the ``/None`` method.
        :raise NotImplementedError:
            Raised when the crypt filter's ``/CFM`` entry indicates an unknown
            crypt filter method.
        )build_crypt_filterrT   )r@   r   r   s      r   read_cf_dictionaryz"SecurityHandler.read_cf_dictionary  s    & "$$fo
 	
r   c                      |j                  dt              |j                  dt              |j                  d      }	 |d    fd}t         |             }t	        ||      S # t        $ r Y y w xY w)N/StmF/StrF/EFF/CFc               3      K   j                         D ]8  \  } }j                  || fv       }|t        j                  d      | |f : y w)Nz!Failed to load crypt filter with )itemsr   r   r   )r   r   cfcf_config_dictr@   stmfstrfs      r   _crypt_filtersz=SecurityHandler.process_crypt_filters.<locals>._crypt_filters  s^      . 4 4 6 f++FDT4L4HI:++,OPPBh	s   AA)crypt_filtersdefault_stream_filterdefault_string_filterdefault_file_filter)ru   IDENTITYrv   re   rW   )r@   rl   effr   r   r   r   r   s   `    @@@r   process_crypt_filtersz%SecurityHandler.process_crypt_filters  s     22vt,	)%0N	 ^-.''"&"& #	
 	
  		s   A0 0	A<;A<methodfactoryc                 "    || j                   |<   y r6   )rT   )r@   r   r   s      r   register_crypt_filterz%SecurityHandler.register_crypt_filter  s     ,3  (r   c                     | j                   }|t        j                  k\  ry|t        j                  k\  ry|t        j                  k\  ryy )N)r   r   )r   r2   )r   r1   )rU   r0   rG   rH   rK   )r:   vs     r   get_min_pdf_versionz#SecurityHandler.get_min_pdf_version  sD    LL&---(666(888r   c                     g }| j                   rddlm} |j                  |       | j                  j                         D ]&  }|j                         }||j                  |       ( |S )Nr   )ISO32004)r   pdfmacr   appendrV   filtersget_extensionsextend)r:   extsr   r   cf_extss        r   r   zSecurityHandler.get_extensions  sd    (KK!**224 	%B'')G"G$	% r   )TTNr6   )r4   r   )1r   r   r   r   rR   r   r.   r   r-   rT   r   
NameObjectr0   r   bytesrb   rg   staticmethodrk   DictionaryObjectr|   rN   ri   r   r   r	   ro   rz   boolr   r<   r(   r   r   r   r   r   r   propertyr   r   r   r   r
   rO   r   r   r   r   r   r   r   rQ   rQ   n   s[   (T CET#t,='>">?DKM$w113GGHM $("'" 8	" 5/"&F d,-   0=G44 0=9J 0= 0=d " " "H-C$D & 
3s8 
 
 ""33" "$ "w77 ""J "9.@" "e   * * * 
--
@D
	-	 
 
, 
"33
	,	-
 
6 3''32F3 3
XeCHo%> %7 8 r   rQ   c                   *   e Zd ZU dZdZed   ed<   dZee   ed<   dZ	d Z
edefd	       Zedej                  fd
       Zdeee      fdZedefd       ZddedefdZddedefdZdej0                  fdZdefdZdefdZd Zedefd       Zy)r   a  
    Generic abstract crypt filter class.

    The superclass only handles the binding with the security handler, and
    offers some default implementations for serialisation routines that may
    be overridden in subclasses.

    There is generally no requirement for crypt filters to be compatible with
    *any* security handler (the leaf classes in this module aren't), but
    the API supports mixin usage so code can be shared.
    NrQ   _handler_shared_keyFc                      || _         d| _        y)z
        Set the security handler to which this crypt filter is tied.

        Called by pyHanko during initialisation.
        N)r   r   r:   handlers     r   _set_security_handlerz!CryptFilter._set_security_handler  s      r   r4   c                     t         )aS  
        Indicate whether authentication previously failed for this crypt filter.

        Note that re-authenticating is not forbidden, this function mostly
        exists to make error reporting easier.

        Crypt filters are allowed to manage their own authentication, but may
        defer to the security handler as well.
        r~   r   s    r   _auth_failedzCryptFilter._auth_failed  s
     "!r   c                     t         )zj
        :return:
            The method name (``/CFM`` entry) associated with this crypt filter.
        r~   r   s    r   r   zCryptFilter.method  s
     "!r   c                      y)zL
        Get applicable developer extensions for this crypt filter.
        Nr   r   s    r   r   zCryptFilter.get_extensions  s     r   c                     t         )zy
        :return:
            The keylength (in bytes) of the key associated with this crypt
            filter.
        r~   r   s    r   r[   zCryptFilter.keylen  r   r   	plaintextc                     t         )aO  
        Encrypt plaintext with the specified key.

        :param key:
            The current local key, which may or may not be equal to this
            crypt filter's global key.
        :param plaintext:
            Plaintext to encrypt.
        :param params:
            Optional parameters private to the crypt filter,
            specified as a PDF dictionary. These can only be used for
            explicit crypt filters; the parameters are then sourced from
            the corresponding entry in ``/DecodeParms``.
        :return:
            The resulting ciphertext.
        r~   r:   keyr   paramss       r   encryptzCryptFilter.encrypt&  
    " "!r   
ciphertextc                     t         )aQ  
        Decrypt ciphertext with the specified key.

        :param key:
            The current local key, which may or may not be equal to this
            crypt filter's global key.
        :param ciphertext:
            Ciphertext to decrypt.
        :param params:
            Optional parameters private to the crypt filter,
            specified as a PDF dictionary. These can only be used for
            explicit crypt filters; the parameters are then sourced from
            the corresponding entry in ``/DecodeParms``.
        :return:
            The resulting plaintext.
        r~   r:   r   r   r   s       r   decryptzCryptFilter.decrypt9  r   r   c                     t        j                  t        j                  d      | j                  rt        j                  d      nt        j                  d      t        j                  d      | j                  i      }|S )a  
        Serialise this crypt filter to a PDF crypt filter dictionary.

        .. note::
            Implementations are encouraged to use a cooperative inheritance
            model, where subclasses first call ``super().as_pdf_object()``
            and add the keys they need before returning the result.

            This makes it easy to write crypt filter mixins that can provide
            functionality to multiple handlers.

        :return:
            A PDF crypt filter dictionary.
        z
/AuthEventz/EFOpenz/DocOpen/CFM)r   r   r   _embedded_onlyr   )r:   results     r   r<   zCryptFilter.as_pdf_objectL  sk     )) ""<0** &&y1 ++J7""6*DKK

 r   c                     t         )a9  
        Compute the (global) file encryption key for this crypt filter.

        :return:
            The key, as a :class:`bytes` object.
        :raise misc.PdfError:
            Raised if the data needed to derive the key is not present (e.g.
            because the caller hasn't authenticated yet).
        r~   r   s    r   derive_shared_encryption_keyz(CryptFilter.derive_shared_encryption_keyh  s
     "!r   c                     | j                   S )aS  
        Derive the encryption key for a specific object, based on the shared
        file encryption key.

        :param idnum:
            ID of the object being encrypted.
        :param generation:
            Generation number of the object being encrypted.
        :return:
            The local key to use for this object.
        )
shared_keyr:   idnum
generations      r   derive_object_keyzCryptFilter.derive_object_keyt  s     r   c                     d| _         y )NT)r   r   s    r   set_embedded_onlyzCryptFilter.set_embedded_only  s
    "r   c                 ~    | j                   }|.| j                  rt        d      | j                         x}| _         |S )z
        Return the shared file encryption key for this crypt filter, or
        attempt to compute it using :meth:`derive_shared_encryption_key`
        if not available.
        zAuthentication failed)r   r   r   r   )r:   r   s     r   r   zCryptFilter.shared_key  sD     ;  -.EFF%)%F%F%HHC$"
r   r6   )r   r   r   r   r   r   r-   r   r   r   r   r   r   r   r   r   r   r   r   r   rO   r[   r   r   r   r<   r   r   r   r   r   r   r   r   r     s   
 -1Hh()0#'K%'N  
"d 
" 
" "** " ".@)A B  " " ""e "U "&"u "e "&w77 8
"e 
"e # E  r   r   c                       e Zd ZdZ ej
                  d      ZdZdZde	fdZ
de	fdZd Zd	 Zdde	de	fdZdde	de	fdZy
)IdentityCryptFiltera>  
    Class implementing the trivial crypt filter.

    This is a singleton class, so all its instances are identical.
    Additionally, some of the :class:`.CryptFilter` API is nonfunctional.
    In particular, :meth:`as_pdf_object` always raises an error, since the
    ``/Identity`` filter cannot be serialised.
    /Noner   Fr4   c                      y)z$Always returns an empty byte string.r   r   r   s    r   r   z0IdentityCryptFilter.derive_shared_encryption_key  s    r   c                      y)z
        Always returns an empty byte string.

        :param idnum:
            Ignored.
        :param generation:
            Ignored.
        :return:
        r   r   r   s      r   r   z%IdentityCryptFilter.derive_object_key  s     r   c                      y)zW
        No-op.

        :param handler:
            Ignored.
        :return:
        Nr   r   s     r   r   z)IdentityCryptFilter._set_security_handler  s     	r   c                 ,    t        j                  d      )zk
        Not implemented for this crypt filter.

        :raise misc.PdfError:
            Always.
        z$Identity filter cannot be serialised)r   rI   r   s    r   r<   z!IdentityCryptFilter.as_pdf_object  s     mmBCCr   Nr   c                     |S )z
        Identity function.

        :param key:
            Ignored.
        :param plaintext:
            Returned as-is.
        :param params:
            Ignored.
        :return:
            The original plaintext.
        r   r   s       r   r   zIdentityCryptFilter.encrypt  s
     r   r   c                     |S )z
        Identity function.

        :param key:
            Ignored.
        :param ciphertext:
            Returned as-is.
        :param params:
            Ignored.
        :return:
            The original ciphertext.
        r   r   s       r   r   zIdentityCryptFilter.decrypt  s
     r   r6   )r   r   r   r   r   r   r   r[   r   r   r   r   r   r<   r   r   r   r   r   r   r     sn      W(FFLe 
e 
De U u e r   r   )	metaclass	/Identityc                       e Zd ZdZeedfdeeef   fdZd Z	d Z
ded   fd	Zdd
Zd Zd Zd Zedej&                  fd       Zedej&                  fd       Zedej&                  fd       Zd Zd Zy)rW   a  
    Crypt filter store attached to a security handler.

    Instances of this class are not designed to be reusable.

    :param crypt_filters:
        A dictionary mapping names to their corresponding crypt filters.
    :param default_stream_filter:
        Name of the default crypt filter to use for streams.
    :param default_stream_filter:
        Name of the default crypt filter to use for strings.
    :param default_file_filter:
        Name of the default crypt filter to use for embedded files.

        .. note::
            PyHanko currently is not aware of embedded files, so managing these
            is the API user's responsibility.
    Nr   c                     dt         ffd}| _        || _        || _        || _         ||      | _         ||      | _        |xs |} ||      | _        y )Nr4   c                 4    | t         k(  r
t               S |    S r6   )r   r   )r   r   s    r   _selectz2CryptFilterConfiguration.__init__.<locals>._select  s)     8# $% #4(r   )r   r   _default_string_filter_name_default_stream_filter_name_default_file_filter_name_default_stream_filter_default_string_filter_default_file_filter)r:   r   r   r   r   r  s    `    r   rb   z!CryptFilterConfiguration.__init__  sg    	[ 	 ,+@(+@()<&&-.C&D#&-.C&D#1J5J$+,?$@!r   c                 d    |t        j                  d      k(  r
t               S | j                  |   S Nr   )r   r   r   r   r:   items     r   __getitem__z$CryptFilterConfiguration.__getitem__  s/    7%%k22&((""4((r   c                 R    |t        j                  d      k(  xs || j                  v S r  )r   r   r   r  s     r   __contains__z%CryptFilterConfiguration.__contains__  s-    G&&{33 +t***	
r   r4   r   c                 6    | j                   j                         S )z2Enumerate all crypt filters in this configuration.)r   rx   r   s    r   r   z CryptFilterConfiguration.filters  s    ""))++r   c                 P    | j                         D ]  }|j                  |        y)z
        Set the security handler on all crypt filters in this configuration.

        :param handler:
            A :class:`.SecurityHandler` instance.
        N)r   r   )r:   r   r   s      r   rZ   z-CryptFilterConfiguration.set_security_handler!  s'     ,,. 	.B$$W-	.r   c                     | j                   S )z
        Retrieve the default crypt filter to use with streams.

        :return:
            A :class:`.CryptFilter` instance.
        )r  r   s    r   r   z'CryptFilterConfiguration.get_for_stream+       ***r   c                     | j                   S )z
        Retrieve the default crypt filter to use with strings.

        :return:
            A :class:`.CryptFilter` instance.
        )r	  r   s    r   r   z'CryptFilterConfiguration.get_for_string4  r  r   c                     | j                   S )z
        Retrieve the default crypt filter to use with embedded files.

        :return:
            A :class:`.CryptFilter` instance.
        )r
  r   s    r   r   z.CryptFilterConfiguration.get_for_embedded_file=  s     (((r   c                     | j                   S zK
        The name of the default crypt filter to use with streams.
        )r  r   s    r   stream_filter_namez+CryptFilterConfiguration.stream_filter_nameF      
 ///r   c                     | j                   S r  )r  r   s    r   string_filter_namez+CryptFilterConfiguration.string_filter_nameM  r  r   c                     | j                   S )zc
        Retrieve the name of the default crypt filter to use with embedded
        files.
        )r  r   s    r   embedded_file_filter_namez2CryptFilterConfiguration.embedded_file_filter_nameT  s     ---r   c           	         t        j                         }| j                  |d<   | j                  |d<   | j                  | j                  |d<   t        j                  | j
                  j                         D ci c]2  \  }}|t        k7  r$t        j                  |      |j                         4 c}}      |d<   |S c c}}w )z
        Serialise this crypt filter configuration to a dictionary object,
        including all its subordinate crypt filters (with the exception of
        the identity filter, if relevant).
        r   r   r   r   )
r   r   r  r  r  r   r   r   r   r<   )r:   r   r   r7   s       r   r<   z&CryptFilterConfiguration.as_pdf_object\  s     ))+::w::w))5!;;F6N00 #'"5"5";";"=C(? ""3')<)<)>>
u s   :7B>
c                 T    | j                   }| j                  }| j                  }|||hS )aa  
        Return the "standard" filters associated with this crypt filter
        configuration, i.e. those registered as the defaults for strings,
        streams and embedded files, respectively.

        These sometimes require special treatment (as per the specification).

        :return:
            A set with one, two or three elements.
        )r  r	  r
  )r:   r   r   r   s       r   standard_filtersz)CryptFilterConfiguration.standard_filtersp  s2     ****''dC  r   )r   rQ   )r   r   r   r   r   r   r.   r   rb   r  r  r   r   rZ   r   r   r   r   r   r   r  r  r  r<   r"  r   r   r   rW   rW     s    , '& AC,-A.)

,-0 ,.++) 0G$6$6 0 0 0G$6$6 0 0 .7+=+= . .(!r   rW   regr   r   r4   c                     	 |d   }|dk(  ry	 | |   } |||      S # t         $ r Y yw xY w# t         $ r t        d|z         w xY w)a@  
    Interpret a crypt filter dictionary for a security handler.

    :param reg:
        A registry of named crypt filters.
    :param cfdict:
        A crypt filter dictionary.
    :param acts_as_default:
        Indicates whether this filter is intended to be used in
        ``/StrF`` or ``/StmF``.
    :return:
        An appropriate :class:`.CryptFilter` object, or ``None``
        if the crypt filter uses the ``/None`` method.
    :raise NotImplementedError:
        Raised when the crypt filter's ``/CFM`` entry indicates an unknown
        crypt filter method.
    r   Nr   zNo such crypt filter method: )rv   r   )r#  r   r   cfmr   s        r   r   r     sq    .Vn g~Ic( 6?++    I!"AC"GHHIs    + 	((A)+enumdataclassesr   typingr   r   r   r   r   r	   r
   r   pyhanko.pdf_utilsr   r    pyhanko.pdf_utils.crypt.cred_serr   #pyhanko.pdf_utils.crypt.permissionsr   pyhanko.pdf_utils.extensionsr   pyhanko.pdf_utils.miscr   r   OrderedEnumr   Enumr#   r(   uniqueVersionEnumr0   rQ   r   	Singletonr   r   r   rW   r   r   rS   r   r   r   r   <module>r3     sM    ! M M M + C > ; /	d// 	!! 499  $  6 *T-- * *Zw wti iXM+ M` 7k*W! W!t w77>KL !,	g  "44	5!,$$!, !, k	!,r   