
    Wwgw                     j   d dl Z d dlZd dlm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mZmZmZmZmZ d dlmZmZmZmZmZ d dlmZ d dlmZ d dlmZmZ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+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1m2Z2 d dl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZCmDZD ddlEmFZF ddlGmHZH ddlImJZJmKZKmLZLmMZMmNZNmOZOmPZP ddlQmRZRmSZSmTZTmUZU g dZV ej                  eX      ZY edeN      ZZdej                  deej                  ej                  df   fdZ^de_fd Z`d!ej                  dej                  fd"Zbd#ej                  d$ej                  d%eej                     d&eej                     fd'Zf	 	 d[d(ej                  d!ej                  d)ehd*eid+eeS   d,ee   dee_e_f   fd-Zjd.ej                  de8fd/Zl	 	 	 	 	 	 d\d.ej                  d0eei   d1ee   d2eem   d3ee-   d4ee/   d5eeS   d6eHde	ehef   fd7Zn	 d]d!ej                  d1ed6eHd8ee-   d4ee/   dd9fd:Zoeddddd;d.ej                  d<eeZ   d0eei   d1ee   d2eem   d6eeH   deZfd=       Zpeddddd;d.ej                  d0eei   d1ee   d2eem   d6eeH   deNfd>       ZpeNdddddfd.ej                  d0eei   d1ee   d2eem   d6eeH   d5eeS   deZfd?Zpd(ej                  dee   fd@Zq	 d^d(ej                  dAe_deej                     fdBZrd(ej                  deei   fdCZsd(ej                  dDee   d0eifdEZt	 d]dFej                  d1ee   dGeid5eeS   fdHZudIeej                     dJej                  d1edeee1   eee&e%e$f      f   fdKZwdLeej                     dJej                  d1ee   dMej                  fdNZxddddde?j                  dfdOeeieej                  ej                  f   d.ej                  dPee   dDee   dQee   d6eeH   d5eeS   deOfdRZ| edSdTU      Z} edTV       G dW dXe
e}                Z~dYee}   de~e}   fdZZy)_    N)	dataclass)datetime)IOAny	AwaitableDictGenericIterableListOptionalTupleTypeTypeVarUnionoverload)algoscmscoretspx509)InvalidSignature)hashes)CancelableAsyncIteratorValidationContextfind_valid_path)DisallowedAlgorithmErrorExpiredErrorInvalidCertificateErrorPathBuildingErrorPathValidationErrorRevokedErrorStaleRevinfoErrorValidationError)TimeSlideFailure)ValidationPath)PKIXValidationParams)ACValidationResultasync_validate_ac)
CMSExtractionErrorCMSStructuralErrorMultivaluedAttributeErrorNonexistentAttributeErrorSignedDataCertscheck_ess_certidextract_certificate_infoextract_signer_infofind_unique_cms_attributeget_pyca_cryptography_hash   )misc)lift_iterable_async   )AdESFailureAdESIndeterminate   )errors)KeyUsageConstraints)CAdESSignerAttributeAssertionsCertifiedAttributesClaimedAttributesRevocationDetailsSignatureStatusStandardCMSSignatureStatusTimestampSignatureStatus)DEFAULT_ALGORITHM_USAGE_POLICYCMSAlgorithmUsagePolicyextract_message_digestvalidate_raw)validate_sig_integrityasync_validate_cms_signaturecollect_timing_infovalidate_tst_signed_dataasync_validate_detached_cmscms_basic_validationcompute_signature_tst_digestextract_tst_dataextract_self_reported_tsextract_certs_for_validationcollect_signer_attr_statusvalidate_algorithm_protectionget_signing_cert_attr
StatusType)boundsigned_attrsreturnc                 >    t        | d      }|t        | d      }|S )a   
    Retrieve the ``signingCertificate`` or ``signingCertificateV2`` attribute
    (giving preference to the latter) from a signature's signed attributes.

    :param signed_attrs:
        Signed attributes.
    :return:
        The value of the attribute, if present, else ``None``.
    T)v2F)_grab_signing_cert_attr)rV   attrs     Z/var/www/horilla/myenv/lib/python3.12/site-packages/pyhanko/sign/validation/generic_cms.pyrS   rS   c   s&     #<D9D|&|>K    rY   c                 0   |rdnd}|rt         j                  nt         j                  }	 t        | |      }|j	                  |j                               S # t        $ r Y y t        $ r-}t        j                  }t        j                  d|      |d }~ww xY w)Nsigning_certificate_v2signing_certificatez3Wrong cardinality for signing certificate attributeades_subindication)r   SigningCertificateV2SigningCertificater1   loaddumpr,   r+   r8   NO_SIGNING_CERTIFICATE_FOUNDr:   SignatureValidationError)rV   rY   	attr_nameclsvalueeerrs          r\   rZ   rZ   u   s     -/(4II&(#
"
"c.D.DC),	B xx

%%$ $ <<--A"
 	s   *A 	B B((BBcertc                     t        |      }|y |d   d   }t        | |      s?t        j                  }t	        j
                  d| j                  j                   d|      y )Ncertsr   zWSigning certificate attribute does not match selected signer's certificate for subject"z".ra   )rS   r.   r8   rg   r:   rh   subjecthuman_friendly)rn   rV   r[   certidrm   s        r\   _check_signing_certificatert      st     !.D| ']1FD&)<<--,,-S2  #	
 	
 *r]   attrsclaimed_digest_algorithm_objclaimed_signature_algorithm_objclaimed_mac_algorithm_objc                    	 t        | d      }||d   j                  }||j                  k7  rt        j                  d      |J|d   j                  }|t        j                  d      ||j                  k7  rt        j                  d      |K|d	   j                  }|t        j                  d
      ||j                  k7  rt        j                  d      yyy# t        $ r d}Y t        $ r t        d      w xY w)a  
    Internal API to validate the CMS algorithm protection attribute
    defined in :rfc:`6211`, if present.

    :param attrs:
        A CMS attribute list.
    :param claimed_digest_algorithm_obj:
        The claimed (i.e. unprotected) digest algorithm value.
    :param claimed_signature_algorithm_obj:
        The claimed (i.e. unprotected) signature algorithm value.
    :param claimed_mac_algorithm_obj:
        The claimed (i.e. unprotected) MAC algorithm value.
    :raises errors.CMSStructuralError:
        if multiple CMS protection attributes are present
    :raises errors.CMSAlgorithmProtectionError:
        if a mismatch is detected
    cms_algorithm_protectionNz4Multiple CMS algorithm protection attributes presentdigest_algorithmzCDigest algorithm does not match CMS algorithm protection attribute.signature_algorithmz<CMS algorithm protection attribute not valid for signed datazFSignature mechanism does not match CMS algorithm protection attribute.mac_algorithmzCCMS algorithm protection attribute not valid for authenticated dataz@MAC mechanism does not match CMS algorithm protection attribute.)r1   r,   r+   r*   nativer:   CMSAlgorithmProtectionError)ru   rv   rw   rx   cms_algid_protectionauth_digest_algorithmauth_sig_algorithmauth_mac_algorithms           r\   rR   rR      s^   0

8- 
 ' 45G H O O $@$G$GG44  +6!5%"f  ")88  $'F'M'MM88,  %0!5o!F!M!M!)88)  $'@'G'GG88,  H 1+ ( % $#$ 
 B
 	

s   C C?+C?signer_infoexpected_content_typeactual_digestalgorithm_usage_policy
time_indicc           
      \   | d   }| d   }|d   j                   }||j                  |||j                        }	|	sUd|d   j                    d}
|	j                  |
d|	j                   d	z  }
t	        j
                  |
|	j                  du 
      |j                  ||      }|sUd|d   j                    d}
|j                  |
d|j                   d	z  }
t	        j
                  |
|j                  du 
      | d   j                   }| d   }|t        j                  u rd}d}|}n| d   j                         }|j                         }d}	 t        |||d       t+        ||       	 t-        |d      }|j                   }||k7  r+t	        j                  d| d| t         j"                        t3        |       }	 t5        ||||||||       d}|||k(  n|}||fS # t        $ r4}t	        j                  |j                  t         j"                        d}~wt        j$                  $ r4}t	        j                  |j                  t&        j(                        d}~ww xY w# t.        t0        f$ r& t	        j                  dt         j"                        w xY w# t6        $ r d}Y w xY w)ae  
    Validate the integrity of a signature for a particular signerInfo object
    inside a CMS signed data container.

    .. warning::
        This function does not do any trust checks, and is considered
        "dangerous" API because it is easy to misuse.

    :param signer_info:
        A :class:`cms.SignerInfo` object.
    :param cert:
        The signer's certificate.

        .. note::
            This function will not attempt to extract certificates from
            the signed data.
    :param expected_content_type:
        The expected value for the content type attribute (as a Python string,
        see :class:`cms.ContentType`).
    :param actual_digest:
        The actual digest to be matched to the message digest attribute.
    :param algorithm_usage_policy:
        Algorithm usage policy.
    :param time_indic:
        Time indication for the production of the signature.
    :return:
        A tuple of two booleans. The first indicates whether the provided
        digest matches the value in the signed attributes.
        The second indicates whether the signature of the digest is valid.
    r|   r{   	algorithmN)moment
public_keyzThe algorithm z, is not allowed by the current usage policy.z	 Reason: .)	permanent)r   	signaturerV   TF)rv   rw   rx   ra   content_typezQContent type not found in signature, or multiple content-type attributes present.zContent type z did not match expected value )	prehashedalgorithm_policyr   )r~   signature_algorithm_allowedr   failure_reasonr:   r   not_allowed_afterdigest_algorithm_allowedr   VOIDuntagrf   rR   r*   rh   failure_messager7   FORMAT_FAILUREr   r8   GENERICrt   r1   r,   r+   rE   rF   r   )r   rn   r   r   r   r   r|   digest_algorithm_objmd_algorithmsig_algo_allowedmsgdigest_algo_allowedr   signed_attrs_origembedded_digestr   signed_datarV   rl   r   validintacts                         r\   rG   rG      s`   N 6A6 ''9:'4;;L)1MM
t N 
   !4[!A!H!H I J> ?   ..:#3#B#B"C1EE11/AATI  5MM  N 
 # !5k!B!I!I J K> ?  #11=#6#E#E"FaHH112DDL  K(//I+6~+FDII%	# #>288: #'')		)-A0C*.	2 	#46		4nL $**0011~-K()+#.#=#=  1=3!		
  & 	(  5=} " 	11!!k6P6P  11 	11!! $5#<#< 	* *+DE 	11&#.#=#= 	8  sB   G  *I% >J  	I")/HI"./II"%5JJ+*J+r   c                     	 t        |       }|j                  }t        |       }|d   }t        ||       |S # t        $ r& t        j                  dt
        j                        w xY w)a  
    Extract certificates from a CMS signed data object for validation purposes,
    identifying the signer's certificate in accordance with ETSI EN 319 102-1,
    5.2.3.4.

    :param signed_data:
        The CMS payload.
    :return:
        The extracted certificates.
    z,signer certificate not included in signaturera   rV   )	r/   signer_certr)   r:   rh   r8   rg   r0   rt   )r   	cert_inforn   r   rV   s        r\   rP   rP     sr     
,[9	$$ &k2K~.Lt\2  
--:0MM
 	

s	   7 /A&
raw_digestvalidation_contextstatus_kwargsvalidation_pathpkix_validation_paramsr   key_usage_settingsc                j  K   t        |       }t        |       }	|	j                  }
|	j                  }d}|/|xs t	        j
                  |j                        }|j                  }|xs
 t               }|t        }|d   }|d   j                  }|d   d   j                  }| d   }|d   j                  }|Pt        |d         }t        |      }t        j                  |      }|j                  |       |j!                         }n:|d   t"        j$                  ur%t'        j(                  dt*        j,                  	      	 t/        ||
||||
      \  }}dx}x}x}}|r	 |j4                  j7                  |       |t9        |g      }n|j:                  j=                  |
      }t?        |
||||       d{   }|j@                  }|jB                  }|jD                  xs |jF                  }|jH                  }|xs i }|dn|jT                  |d<   |j                  |||
||||||	       |S # t0        $ r8}t'        j(                  d|j2                  z   t*        j,                  	      |d}~ww xY w7 # tJ        $ r1}tL        jO                  d|       tP        jR                  }Y d}~d}~ww xY ww)z
    Perform basic validation of CMS and PKCS#7 signatures in isolation
    (i.e. integrity and trust checks).

    Internal API.
    Nr|   r   r{   encap_content_infor   contentzKCMS structural error: detached signatures should not have encapsulated datara   )r   r   r   r   zCMS structural error: )r   pathsr   z&Processing error in validation processexc_infovalidation_time)	r   r   signing_certr   pkcs7_signature_mechanismtrust_problem_indicr   revocation_detailserror_time_horizon)+r0   rP   r   other_certsrD   lift_policyr   best_signature_timer   rC   r~   bytesr2   r   Hashupdatefinalizer   r   r:   rh   r7   r   rG   r*   r   certificate_registryregister_multipler5   path_builderasync_build_paths_lazyvalidate_cert_usageerror_subindicrevo_detailssuccess_result
error_pathr   
ValueErrorloggererrorr8   !CERTIFICATE_CHAIN_GENERAL_FAILUREr   )r   r   r   r   r   r   r   r   r   r   rn   r   r   r|   	mechanismr   ecir   rawmd_specmdr   r   rl   ades_statuspathr   r   r   	op_results                                 r\   rL   rL     s	    " &k2K,[9I  D''KJ% &22"33 	 (;;
+B/@/B95@6 $K077I12;?FFL
*
+C/66C	N#,\:[[!
		#[[]
	Ytyy	(-- *99
 	
."7$#3!
 >BAKA$A(:	N33EE
 *+_,=>*77NN 2"#5'= I $22K$11L++Cy/C/CD!*!=!=
 "'RM"*0B0I0I #$ !"+''-  
 i  --$q'8'88*99
 	,  	NLLAALN+MMK	Nsh   D>J3H0 
J3 AI6 8I49AI6 ;5J30	I193I,,I11J34I6 6	J0?'J+&J3+J00J3r   z,CertvalidatorOperationResult[ValidationPath]c                 h    K   dt         f fd}t         |              d{   S 7 w)zE
    Low-level certificate validation routine.
    Internal API.
    rW   c                  `   K   j                          t                d {   S 7 w)N)r   r   )validater   )rn   r   r   r   r   s   r\   _checkz#validate_cert_usage.<locals>._check9  s8     ##D)$1#9	
 
 	
 
s   $.,.N)r%   handle_certvalidator_errors)rn   r   r   r   r   r   s   ````` r\   r   r   -  s-     
. 
 
 -VX6666s   $202)r   r   r   r   
status_clsc                   K   y wN )r   r   r   r   r   r   s         r\   rH   rH   F  s         c                   K   y wr   r   )r   r   r   r   r   s        r\   rH   rH   R  s      r   c                 r   K   |j                  |      }t        | |||||       d{   } |di |S 7 w)a  
    Validate a CMS signature (i.e. a ``SignedData`` object).

    :param signed_data:
        The :class:`.asn1crypto.cms.SignedData` object to validate.
    :param status_cls:
        Status class to use for the validation result.
    :param raw_digest:
        Raw digest, computed from context.
    :param validation_context:
        Validation context to validate the signer's certificate.
    :param status_kwargs:
        Other keyword arguments to pass to the ``status_class`` when reporting
        validation results.
    :param key_usage_settings:
        A :class:`.KeyUsageConstraints` object specifying which key usages
        must or must not be present in the signer's certificate.
    :param algorithm_policy:
        The algorithm usage policy for the signature validation.

        .. warning::
            This is distinct from the algorithm usage policy used for
            certificate validation, but the latter will be used as a fallback
            if this parameter is not specified.

            It is nonetheless recommended to align both policies unless
            there is a clear reason to do otherwise.
    :return:
        A :class:`.SignatureStatus` object (or an instance of a proper subclass)
    )r   r   Nr   )default_usage_constraintsrL   )r   r   r   r   r   r   r   eff_key_usage_settingss           r\   rH   rH   ]  sU     N (AA /1) M &&&s   &757c                 h    	 | d   }t        |d      }|j                  S # t        t        f$ r Y yw xY w)a  
    Extract self-reported timestamp (from the ``signingTime`` attribute)

    Internal API.

    :param signer_info:
        A ``SignerInfo`` value.
    :return:
        The value of the ``signingTime`` attribute as a ``datetime``, or
        ``None``.
    rV   signing_timeN)r1   r~   r,   r+   )r   sasts      r\   rO   rO     s?    (&r>:yy%'@A s    11signedc                     	 |r| d   }t        |d      }n| d   }t        |d      }|d   }|S # t        t        f$ r Y yw xY w)a  
    Extract signed data associated with a timestamp token.

    Internal API.

    :param signer_info:
        A ``SignerInfo`` value.
    :param signed:
        If ``True``, look for a content timestamp (among the signed
        attributes), else look for a signature timestamp (among the unsigned
        attributes).
    :return:
        The ``SignedData`` value found, or ``None``.
    rV   content_time_stampunsigned_attrssignature_time_stamp_tokenr   N)r1   r,   r+   )r   r   r   tstuatst_signed_datas         r\   rN   rN     s`    "
^,B+B0DEC-.B+B0LMCi.%'@A s   +. A A c                    t        |       }|y|d   }|d   j                  d   }|d   d   j                  }| d   j                  }t        |      }t	        j
                  |      }|j                  |       |j                         S )a.  
    Compute the digest of the signature according to the message imprint
    algorithm information in a signature timestamp token.

    Internal API.

    :param signer_info:
        A ``SignerInfo`` value.
    :return:
        The computed digest, or ``None`` if there is no signature timestamp.
    Nr   r   message_imprinthash_algorithmr   r   )rN   parsedr~   r2   r   r   r   r   )r   tst_datar   mitst_md_algorithmsignature_bytestst_md_specr   s           r\   rM   rM     s      ,H
'
(C	Y		0	1B*+K8??!+.55O,-=>K	[	!BIIo;;=r]   ts_validation_contextc                 .  K   i }t        |       }|||d<   t        | d      }|4t        |       }|J t        |||       d{   }t	        d	i |}||d<   t        | d      }	|	&t        |	||       d{   }
t	        d	i |
}||d<   |S 7 K7 w)
a  
    Collect and validate timing information in a ``SignerInfo`` value.
    This includes the ``signingTime`` attribute, content timestamp information
    and signature timestamp information.

    :param signer_info:
        A ``SignerInfo`` value.
    :param ts_validation_context:
        The timestamp validation context to validate against.
    :param raw_digest:
        The raw external message digest bytes (only relevant for the
        validation of the content timestamp token, if there is one)
    Nsigner_reported_dtF)r   timestamp_validityT)expected_tst_imprintcontent_timestamp_validityr   )rO   rN   rM   rJ   rB   )r   r   r   r   r   r   tst_signature_digesttst_validity_kwargstst_validitycontent_tst_signed_datacontent_tst_validity_kwargscontent_tst_validitys               r\   rI   rI     s     & %'M 2+>%.@*+&{5AO";KH#///$<! %
 

 0F2EF.:*+.{4H*,D#!!+-
 '
#
  8  
) 
 7K23)
'
s$   ABB4B:B;BBr   r   c                   K   d}| d   d   }t        |t        j                        r|j                  }t        |t        j
                        s%t        j                  dt        j                        |d   j                  }t        j                         }t        | |d|i||       d{   }|d	   d
   j                  }	||	k7  r=t        j                  d|	j!                          d|j!                          d       d|d<   |S 7 Zw)a  
    Validate the ``SignedData`` of a time stamp token.

    :param tst_signed_data:
        The ``SignedData`` value to validate; must encapsulate a ``TSTInfo``
        value.
    :param validation_context:
        The validation context to validate against.
    :param expected_tst_imprint:
        The expected message imprint value that should be contained in
        the encapsulated ``TSTInfo``.
    :param algorithm_policy:
        The algorithm usage policy for the signature validation.

        .. warning::
            This is distinct from the algorithm usage policy used for
            certificate validation, but the latter will be used as a fallback
            if this parameter is not specified.

            It is nonetheless recommended to align both policies unless
            there is a clear reason to do otherwise.
    :return:
        Keyword arguments for a :class:`.TimeStampSignatureStatus`.
    Nr   r   z'SignedData does not encapsulate TSTInfora   gen_time	timestamp)r   r   r   r   r   hashed_messagezTimestamp token imprint is z, but expected r   Fr   )
isinstancer   ParsableOctetStringr   r   TSTInfor:   rh   r7   r   r~   rB   r   rL   r   warninghex)
r   r   r   r   tst_infotst_info_bytesr  ku_settingsr   tst_imprints
             r\   rJ   rJ     s    > H$%9:9EN.$":":;!((h,--5*99
 	
 $++I*DDFK.-"I.&) M ,-.>?FFK{*)+//*;)<O#'')*!-	
 #(h!s   B(D*D+ADacsr   c                 ,  K   | D cg c]  }t        |||       }}g }g }t        j                  |      D ]  }	 |j                  | d {           ||fS c c}w 7 # t        t
        t        f$ r} |j                  |       Y d }~Ud }~ww xY ww)N)holder_cert)r(   asyncioas_completedappendr   r    r   )	r  r   r   acjobsresultsr:   jobrl   s	            r\   process_certified_attrsr  Y  s       	"0kJD  GF##D) 	NN9% F? %#
 	
 FMM!	sJ   BABA!A
A!BA!!B5BBBBsd_attr_certificatessd_signed_attrsc                   K   	 t        |d      }i }d }d }||d   }	t        j                  t        |	t        j                        s|	nd      }
|d   }d}t        |t        j                        s[|D cg c]  }|j                  dk(  r|j                   }}t        |      t        |      k7  }|t!        |||      }| d {   \  }}|t#        j$                  |      }nd }|xs t        |d   t        j                         }||rt&        j)                  d	       t+        |
|||
      |d<   |[t!        | ||       d {   \  }}|r|j-                  |       |r|j-                  |       t#        j$                  |      |d<   ||d<   |S # t        $ r d }Y t        $ r4}t        j                  t        |      t        j                        |d }~ww xY wc c}w 7  7 w)Nsigner_attributes_v2ra   claimed_attributesr   certified_attributes_v2F	attr_certsigned_assertionszCAdES signer attributes with externally certified assertions for which no validation method is available. This may affect signature semantics in unexpected ways.)claimed_attrscertified_attrsac_validation_errsunknown_attrs_presentcades_signer_attrsac_attrsr(  )r1   r,   r+   r:   rh   strr7   r   r>   from_iterabler
  r   Voidnamechosenlenr  r=   from_resultsr   r  r<   extend)r  r   r   r  signer_attrsrl   resultcades_ac_resultscades_ac_errorsclaimed_asn1claimedcertified_asn1unknown_cert_attrsr[   	cades_acsval_job	certifiedunknown_attrs
ac_results	ac_errorss                       r\   rQ   rQ   u  s8    
03
  FO#$89 $11 *< CL
 &&?@".$))4
 +99+ I  "%Y3~3F!F!-1&
 ;BM1 / ' ,889IJII
 + 
*,-tyy3
 /
 )mNN: (F!%."/	(
#$ % '> +/A'
 !

I
 ./_-0==jIz'0#$Mo % $ --F{'A'A
	2 5BF!
s`   G(F A$G(4"G-G(G#B G(G&AG(GG(G'/GG	G(&G(
input_datasigner_validation_contextac_validation_contextc	           	      L  K   ||}t        |      }	|	d   d   j                  }
t        j                  t	        |
            }t        | t              r|j                  |        nlt        | t        j                  t        j                  f      r|j                  t        | d                n$t        |      }t        j                  || ||       |j                         }t        |	||       d{   }t!        j"                  |      }t%        ||||||       d{   }t'        |      }|%|j(                  j+                  |j,                         |j                  t/        |j0                  |j2                  ||	d   	       d{          t!        d
i |S 7 7 7 w)a  
    .. versionadded: 0.9.0

    .. versionchanged: 0.11.0
        Added ``ac_validation_context`` param.

    Validate a detached CMS signature.

    :param input_data:
        The input data to sign. This can be either a :class:`bytes` object,
        a file-like object or a :class:`cms.ContentInfo` /
        :class:`cms.EncapsulatedContentInfo` object.

        If a CMS content info object is passed in, the `content` field
        will be extracted.
    :param signed_data:
        The :class:`cms.SignedData` object containing the signature to verify.
    :param signer_validation_context:
        Validation context to use to verify the signer certificate's trust.
    :param ts_validation_context:
        Validation context to use to verify the TSA certificate's trust, if
        a timestamp token is present.
        By default, the same validation context as that of the signer is used.
    :param ac_validation_context:
        Validation context to use to validate attribute certificates.
        If not supplied, no AC validation will be performed.

        .. note::
            :rfc:`5755` requires attribute authority trust roots to be specified
            explicitly; hence why there's no default.
    :param algorithm_policy:
        The algorithm usage policy for the signature validation.

        .. warning::
            This is distinct from the algorithm usage policy used for
            certificate validation, but the latter will be used as a fallback
            if this parameter is not specified.

            It is nonetheless recommended to align both policies unless
            there is a clear reason to do otherwise.
    :param key_usage_settings:
        Key usage parameters for the signer.
    :param chunk_size:
        Chunk size to use when consuming input data.
    :param max_read:
        Maximal number of bytes to read from the input stream.
    :return:
        A description of the signature's status.
    Nr{   r   r   )max_read)r   r   )r   r   r   r   r   rV   )r  r   r   r  r   )r0   r~   r   r   r2   r
  r   r   r   ContentInfoEncapsulatedContentInfo	bytearrayr4   chunked_digestr   rI   rA   r   rL   r/   r   r   r   rQ   attribute_certsr   )rB  r   rC  r   rD  r   r   
chunk_sizerF  r   r{   htemp_bufdigest_bytesr   r   s                   r\   rK   rK     s    z $ 9%k2K"#56{CJJ./?@AA*e$		J#2M2M N	O	z),-.Z(Hj!hG::<L-3 M
 4MM /4#-) M )5I(22DD!!	
 (!*!:!:!--4'7	
 	
 &666;	
s7   C/F$1F2-F$F  A*F$
F"F$ F$"F$
ResultTypeT)	covariant)frozenc                   r    e Zd ZU dZee   ed<   dZee   ed<   dZ	ee
   ed<   dZee   ed<   dZee   ed<   y)CertvalidatorOperationResultzB
    Internal class to inspect error data from certvalidator.
    r   Nr   r   r   r   )__name__
__module____qualname____doc__r   rP  __annotations__r   r?   r   r   r   r%   r   r8   r   r]   r\   rT  rT  H  sS     Z((04L(,-4-1*1+/J(/26NH./6r]   rT  coroc                 J  K   d}dx}}	 t        |  d{         S 7 	# t        $ r<}t        j                  |j                  |       t
        j                  }Y d}~n3d}~wt        $ r<}t        j                  |j                  |       t
        j                  }Y d}~nd}~wt        $ rH}t        j                  |j                  |       t
        j                  }|j                  }Y d}~nd}~wt        $ rq}t        j                  |j                  |       |j                  }|j                  t
        j                  }nt
        j                   }|j                  }Y d}~n)d}~wt"        $ r}|j                  }t        j                  |j                         |j$                  }|j&                  rt
        j(                  }nq|j*                  r3t
        j,                  }t/        d|j$                  |j0                        }n2t
        j2                  }t/        d|j$                  |j0                        }Y d}~nRd}~wt4        $ r2}t        j                  d|       t
        j6                  }Y d}~nd}~wt8        $ rz}|j                  }t        j                  |j                         |j:                  }|j&                  s|j*                  rt
        j<                  }nt
        j(                  }Y d}~nd}~wt>        $ rG}|j                  }t        j                  |j                  |       t
        j(                  }Y d}~nJd}~wt@        $ r;}t        j                  |j                  |       t
        j(                  }Y d}~nd}~ww xY wt        d||||      S w)	z
    Internal error handling function that maps certvalidator errors
    to AdES status indications.

    :param coro:
    :return:
    N)r   r   F)
ca_revokedrevocation_daterevocation_reasonTzFailed to build path)r   r   r   r   r   )!rT  r   r   r  failure_msgr8   CHAIN_CONSTRAINTS_FAILUREr$   NO_POEr"   	TRY_LATERtime_cutoffr   original_pathbanned_sinceCRYPTO_CONSTRAINTS_FAILURE!CRYPTO_CONSTRAINTS_FAILURE_NO_POEr!   revocation_dtis_side_validationr   
is_ee_certREVOKED_NO_POEr?   reasonREVOKED_CA_NO_POEr   NO_CERTIFICATE_CHAIN_FOUNDr   
expired_dtOUT_OF_BOUNDS_NO_POEr    r#   )rZ  time_horizonr   r   rl   r   s         r\   r   r   U  s     (,LL4FJ+:FF:" Bq}}q1'AA /q}}q1'.. %q}}q1 (11}}# *q}}q1 >>!+FFK ,MMK>>L q}}%+MMK\\+::K,  !"#((L ,==K, !"#((L
  C-:'BB Nq}}%||##
 ,@@K+MMK Jq}}q1'II Jq}}q1'IIJ (!'" s   N#
    N#  	N1A N# N,1B#N##N/=C2,N#2N>A&E*$N#*N6CI;N#N'I:4N#:NA0K;6N#;N=M	N#	N1NN#NN#)NN)NNNNNNr   )F)r  loggingdataclassesr   r   typingr   r   r   r   r	   r
   r   r   r   r   r   r   r   
asn1cryptor   r   r   r   r   cryptography.exceptionsr   cryptography.hazmat.primitivesr   pyhanko_certvalidatorr   r   r   pyhanko_certvalidator.errorsr   r   r   r   r    r!   r"   r#    pyhanko_certvalidator.ltv.errorsr$   pyhanko_certvalidator.pathr%   !pyhanko_certvalidator.policy_declr&   pyhanko_certvalidator.validater'   r(   pyhanko.sign.generalr)   r*   r+   r,   r-   r.   r/   r0   r1   r2   	pdf_utilsr4   pdf_utils.miscr5   ades.reportr7   r8    r:   settingsr;   statusr<   r=   r>   r?   r@   rA   rB   utilsrC   rD   rE   rF   __all__	getLoggerrU  r   rT   CMSAttributesrd   rc   rS   boolrZ   Certificatert   DigestAlgorithmSignedDigestAlgorithmHmacAlgorithmrR   
SignerInfor,  r   rG   
SignedDatarP   dictrL   r   rH   rO   rN   rM   rI   rJ   AttributeCertificateV2r  rQ   DEFAULT_CHUNK_SIZErG  rH  rK   rP  rT  r   r   r]   r\   <module>r     s     !      3 2 4 1 
	 	 	 > 5 B P    1 8  )     
		8	$\9
##
3!!3#;#;TAB$d ,




*-*;*;
2CC"%"5"5C &.e.I.I%JC  ((;(;<	CV AE%)ee


e e 	e
 %%<=e "e 4:ePB #'6:$(04=A:>yyy !!23y D>	y
 n-y %%9:y 67y ,y 
#s(^yB >B7


7)7 ,7 #>2	7
 %%9:7 472 

 #'6:$(8< Z  	
 !!23 D> !!45  
 
 #'6:$(8<  !!23	
 D> !!45  
 "&6:$(8<:>3'3' 3' !!23	3'
 D>3' !!453' 673' 3'l#.. Xh=O * 16)-cnn<e_B22#$562 2r ;?	;^^; !23;  ; 67	;|	#,,	-!! * 	!#46MMN	8b"3#=#=>b!!b !!23b &&	bP >B9=9=8<:>&&h7eR#2M2MMNh7h7  ((9:h7 $$56	h7
 $$56h7 !!45h7 67h7  h7V \T2
 $	77:#6 	7 	7Z
J
Z!*-Zr]   