
    Wwgp                        d dl 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
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mZ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(m)Z) ddl*m+Z+m,Z, ddl-m.Z. g dZ/ e j`                  e1      Z2 ed       G d d             Z3 ed       G d d             Z4 ed       G d de4             Z5 ed       G d d             Z6 ed       G d de6             Z7d ee8   d!e8d"e9d#e:fd$Z; G d% d&      Z< G d' d(      Z= ed       G d) d*             Z> ed       G d+ d,             Z? ed       G d- d.e?e4             Z@e G d/ d0e"             ZA ed       G d1 d2             ZB ed       G d3 d4eBe@             ZC ed       G d5 d6eBe5             ZDy)7    N)defaultdict)	dataclass)datetime)unique)
AnyClassVar
CollectionDictIterableListOptionalSetTupleUnion)cmscorecrlkeysx509)PathBuildingErrorPathValidationErrorValidationError)ValidationPath)ACValidationResult   )OrderedEnum   )AdESFailureAdESSubIndic)
DiffResultModificationLevelSuspiciousModification   )SignatureValidationErrorSigSeedValueValidationError)KeyUsageConstraints)SignatureStatusTimestampSignatureStatusX509AttributeInfoCertifiedAttributeInfoClaimedAttributesCertifiedAttributesCAdESSignerAttributeAssertionsStandardCMSSignatureStatusSignatureCoverageLevelModificationInfoPdfSignatureStatusDocumentTimestampStatusRevocationDetailsSignerAttributeStatusT)frozenc                   H    e Zd ZU dZeed<   	 eed<   	 ej                  ed<   y)r3   zQ
    Contains details about a certificate revocation related to a signature.
    
ca_revokedrevocation_daterevocation_reasonN)	__name__
__module____qualname____doc__bool__annotations__r   r   	CRLReason     U/var/www/horilla/myenv/lib/python3.12/site-packages/pyhanko/sign/validation/status.pyr3   r3   :   s2     
  }}$rB   r3   c                   v   e Zd ZU dZeed<   	 eed<   	 ee   ed<   	 ej                  ed<   	 e
ed<   	 e
ed<   	 ee   ed<   	 ee   ed	<   	 ee   ed
<   	 dhZeee
      ed<   	 dZeeee
         ed<   	 ee   ed<   	 d Zedefd       Zedefd       Zdde
fdZe	 ddee   defd       Zede
fd       Zy)r'   zE
    Class describing the validity of a (general) CMS signature.
    intactvalidtrust_problem_indicsigning_certpkcs7_signature_mechanismmd_algorithmvalidation_pathrevocation_detailserror_time_horizonnon_repudiation	key_usageNextd_key_usagevalidation_timec              #   T   K   | j                   rd}n| j                  rd}nd}| y w)NTRUSTEDREVOKED	UNTRUSTED)trustedrevoked)selfcert_statuss     rC   summary_fieldszSignatureStatus.summary_fields   s)     <<#K\\#K%Ks   &(returnc                     | j                   duS )z
        Reports whether the signer's certificate has been revoked or not.
        If this field is ``True``, then obviously :attr:`trusted` will be
        ``False``.
        N)rL   rX   s    rC   rW   zSignatureStatus.revoked   s     &&d22rB   c                 v    | j                   xr, | j                  xr | j                  du xr | j                  duS )z
        Reports whether the signer's certificate is trusted w.r.t. the currently
        relevant validation context and key usage requirements.
        N)rF   rE   rG   rK   r]   s    rC   rV   zSignatureStatus.trusted   sD     JJ 11((D01 $$D0		
rB   c                 x    | j                   r.| j                  r"d|j                  | j                               z   S y)zQ
        Provide a textual but machine-parsable summary of the validity.
        zINTACT:INVALID)rE   rF   joinrZ   )rX   	delimiters     rC   summaryzSignatureStatus.summary   s1     ;;4::y~~d.A.A.CDDDrB   key_usage_settingsc                     |xs
 t               }t        |j                  | j                  n|j                  |j                  | j                  n|j                        }|S )N)rO   rP   )r&   rO   rP   )clsrd   s     rC   default_usage_constraintsz)SignatureStatus.default_usage_constraints   sh     0H3F3H0 &//7 '11 &44< ""'66
 "!rB   c                 f    | j                   %| j                   d   }|j                  j                  S y)Nr   zNo path to trust anchor found.)rK   subjecthuman_friendly)rX   trust_anchors     rC   _trust_anchorzSignatureStatus._trust_anchor   s3    +-1-A-A!-DL''6663rB   ),N)r:   r;   r<   r=   r>   r?   r   r   r   Certificatestrr   r3   r   rO   r   r   rP   rZ   propertyrW   rV   rc   classmethodr&   rg   rl   rA   rB   rC   r'   r'   Q   sr    L K ",//
 """  #"
  n--
 !!233
 !** &7$7IxC!7
 48NHXc#h/07 h'' 3 3 3 

 

 

  AE"!)*=!>"	" "$ 4s 4 4rB   r'   c                   R    e Zd ZU dZ e       Zeee      e	d<   	 dhZ
	 ee	d<   	 d Zy)r(   zG
    Signature status class used when validating timestamp tokens.
    rO   time_stamping	timestampc                 6   | j                   }d| j                  r| j                  rdnd d|j                  j                   d|j
                  j                          d|j                  j                          d| j                   d| j                  rd d	S d d	S )
Nz`This timestamp is backed by a time stamping authority.
The timestamp token is cryptographically  unz!sound.
TSA certificate subject: "z$"
TSA certificate SHA1 fingerprint: z%
TSA certificate SHA256 fingerprint: z
TSA cert trust anchor: "z"
The TSA certificate is ztrusted.)
rH   rE   rF   ri   rj   sha1hexsha256rl   rV   )rX   tsas     rC   describe_timestamp_trustz1TimestampSignatureStatus.describe_timestamp_trust  s    8[[TZZrT: ;**-++*D*D)E F1140@ A336::>>3C2D E((,(:(:'; <&\\r,H6
	
 (,,H6
	
rB   N)r:   r;   r<   r=   setrO   r   r   rp   r?   rP   r   r}   rA   rB   rC   r(   r(      sD     %(EIxC!) &&N
 
rB   r(   c                   V    e Zd ZU dZej
                  ed<   	 eej                     ed<   y)r)   z%
    Info on an X.509 attribute.
    	attr_typeattr_valuesN)
r:   r;   r<   r=   r   AttCertAttributeTyper?   r   r   	Asn1ValuerA   rB   rC   r)   r)     s0     ''' $..))rB   r)   c                   "    e Zd ZU dZee   ed<   y)r*   zI
    Info on a certified attribute, including AC validation results.
    validation_resultsN)r:   r;   r<   r=   r   r   r?   rA   rB   rC   r*   r*   #  s     !!344rB   r*   r   	attr_kinderrfatalc                     | sdnd|  d}d| d| d|j                   d    }|rt        |t        j                        |t        j                  ||	       y )
Nzunknown typeztype ''zFailed to parse z of z: r   )ades_subindication)exc_info)argsr$   r   FORMAT_FAILUREloggerwarning)r   r   r   r   attr_type_strmsgs         rC   _handle_attr_errr   1  sf     +4N6)A9NMYKtM?"SXXa[M
JC&K$>$>
	 	sS)rB   c                   j    e Zd ZdZe	 ddee   fd       ZddZde	de
fdZd Zd	 Zd
 Zde	defdZy)r,   zJ
    Container class for extracted attribute certificate information.
    resultsc           	         t        d       }|D ]i  }|j                  j                         D ]J  }|d   j                  }	 t	        |d         }||   \  }	}
|	j                  |       |
j                  |       L k t               }|j                         D ]H  \  }\  }	}
t        t        j                  |      t        |	      t        |
            |j                  |<   J |S # t
        $ r}t        |d||       Y d }~d }~ww xY w)Nc                  
    g g fS rn   rA   rA   rB   rC   <lambda>z2CertifiedAttributes.from_results.<locals>.<lambda>I  s
    R rB   typevalueszcertified attributer   )r   r   r   )r   approved_attributesr   nativelist
ValueErrorr   extendappendr,   itemsr*   r   r   tuple_attrs)rf   r   parse_error_fatalby_typeresultattrr   r   etype_valuestype_resultsinfoss               rC   from_resultsz CertifiedAttributes.from_resultsC  s    () 	  	,F2299; , L//		!$x.1F -4I,>)\""6*##F+,	," $%6=mmo 	2I2\&<229=!+.#(#6	'ELL#	 ) " $!-/	 s   C""	D+C??Dc                     i | _         y rn   r   r]   s    rC   __init__zCertifiedAttributes.__init__f  s	    9;rB   itemr[   c                      | j                   |   S rn   r   rX   r   s     rC   __getitem__zCertifiedAttributes.__getitem__i      {{4  rB   c                 ,    t        | j                        S rn   lenr   r]   s    rC   __len__zCertifiedAttributes.__len__l      4;;rB   c                 ,    t        | j                        S rn   r>   r   r]   s    rC   __bool__zCertifiedAttributes.__bool__o      DKK  rB   c                 H    t        | j                  j                               S rn   iterr   r   r]   s    rC   __iter__zCertifiedAttributes.__iter__r      DKK&&())rB   c                     || j                   v S rn   r   r   s     rC   __contains__z CertifiedAttributes.__contains__u      t{{""rB   NF)rX   r,   )r:   r;   r<   r=   rr   r   r   r   r   rp   r*   r   r   r   r   r>   r   rA   rB   rC   r,   r,   >  sh     FK 12   D<! !(> ! !*# # #rB   r,   c                   ~    e Zd ZdZe	 ddeej                     fd       ZddZ	de
defdZd Zd	 Zd
 Zde
defdZy)r+   z|
    Container class for extracted information on attributes asserted
    by a signer without an attribute certificate.
    attrsc                    t               }t        t              }|D ]6  }d }	 |d   j                  }t        |d         }||   j                  |       8 |j                         D ];  \  }}	t        t        j                  |      t        |	            |j                  |<   = |S # t        $ r}t        |d||       Y d }~d }~ww xY w)Nr   r   zclaimed attributer   )r   r   )r+   r   r   r   r   r   r   r   r)   r   r   r   r   )
rf   r   r   r   r   r   r   r   r   r   s
             rC   from_iterablezClaimedAttributes.from_iterable  s     "#d# 
	.DI L//	d8n- I%%f-
	. '.mmo 	"I{&7229=!+.'ELL#	    2A=N 	s   B%%	C.CCc                     i | _         y rn   r   r]   s    rC   r   zClaimedAttributes.__init__  s	    46rB   r   r[   c                      | j                   |   S rn   r   r   s     rC   r   zClaimedAttributes.__getitem__  r   rB   c                 ,    t        | j                        S rn   r   r]   s    rC   r   zClaimedAttributes.__len__  r   rB   c                 ,    t        | j                        S rn   r   r]   s    rC   r   zClaimedAttributes.__bool__  r   rB   c                 H    t        | j                  j                               S rn   r   r]   s    rC   r   zClaimedAttributes.__iter__  r   rB   c                     || j                   v S rn   r   r   s     rC   r   zClaimedAttributes.__contains__  r   rB   Nr   )rX   r+   )r:   r;   r<   r=   rr   r   r   AttCertAttributer   r   rp   r)   r   r   r   r   r>   r   rA   rB   rC   r+   r+   y  sl    
 FKS112 27! !(9 ! !*# # #rB   r+   c                   z    e Zd ZU dZeed<   	 dZee   ed<   	 dZ	ee
eeef         ed<   	 dZeed<   	 ed        Zy)	r-   z
    Value type describing information extracted (and, if relevant, validated)
    from a ``signer-attrs-v2`` signed attribute.
    claimed_attrsNcertified_attrsac_validation_errsFunknown_attrs_presentc                     | j                    S rn   )r   r]   s    rC   rF   z$CAdESSignerAttributeAssertions.valid  s    ****rB   )r:   r;   r<   r=   r+   r?   r   r   r,   r   r	   r   r   r   r   r>   rq   rF   rA   rB   rC   r-   r-     s    
 %$
 6:OX129 	 5*;;<=  #(4' + +rB   r-   c                   ^    e Zd ZU dZee   ed<   	 dZeee	e
ef         ed<   	 dZee   ed<   y)r4   Nac_attrsr   cades_signer_attrs)r:   r;   r<   r   r   r,   r?   r   r	   r   r   r   r   r-   rA   rB   rC   r4   r4     s`    .2Hh*+2( 	 5,.??@A  DH!?@GrB   r4   c                        e Zd ZU dZdZee   ed<   	 dZee	   ed<   	 dZ
ee	   ed<   	 edefd       Z fdZd	 Zdeeeef      fd
Z xZS )r.   zs
    Status of a standard "end-entity" CMS signature, potentially with
    timing information embedded inside.
    Nsigner_reported_dttimestamp_validitycontent_timestamp_validityr[   c                 F   | j                   }|d}n(|j                  xr |j                  xr |j                  }| j                  }|d}n(|j                  xr |j                  xr |j                  }| j                  xr" | j                  xr | j                  xr |xr |S )ab  
        Formulates a general judgment on the validity of this signature.
        This takes into account the cryptographic validity of the signature,
        the signature's chain of trust and the validity of the timestamp token
        (if present).

        :return:
            ``True`` if all constraints are satisfied, ``False`` otherwise.
        T)r   rF   rE   rV   r   )rX   tstimestamp_ok
content_tscontent_timestamp_oks        rC   bottom_linez&StandardCMSSignatureStatus.bottom_line  s     $$:L88@		@bjjL44
#'    MZ%6%6M:;M;M !
 KK %

%% % %	
rB   c              #   B  K   t         |          E d {    | j                  !d| j                  j                  d      z   | j                  !d| j                  j                  d      z   | j
                  | j
                  j                  sd y y y 7 w)NzTIMESTAMP_TOKEN<%s>|)rb   zCONTENT_TIMESTAMP_TOKEN<%s>CERTIFIED_SIGNER_ATTRS_INVALID)superrZ   r   rc   r   r   rF   rX   	__class__s    rC   rZ   z)StandardCMSSignatureStatus.summary_fields?  s     7)+++"".'''//#/>  **6///77#7F  ##/++1122 2 0 	,s   BBBBc                     d | j                         }d| j                  rdnd d}|j                  d|f       dj                  fd|D              S )	Nc                 D    dj                  | dt        |       z  |df      S )N
-)ra   r   )hdrbodys     rC   fmt_sectionzDStandardCMSSignatureStatus.pretty_print_details.<locals>.fmt_sectionQ  s"    99c3S>4>??rB   zThe signature is judged rw   INzVALID.zBottom liner   c              3   6   K   | ]  \  }} ||        y wrn   rA   ).0r   r   r   s      rC   	<genexpr>zBStandardCMSSignatureStatus.pretty_print_details.<locals>.<genexpr>Y  s     JICS$/Js   )pretty_print_sectionsr   r   ra   )rX   sectionsr   r   s      @rC   pretty_print_detailsz/StandardCMSSignatureStatus.pretty_print_detailsP  s]    	@ --/&T-=-=r4&HO 	 	45yyJJJJrB   c                    | j                   }| j                  rd}n| j                  rd}nd}d|j                  j                   d|j
                  j                          d|j                  j                          d| j                   d| d	}d
| j                  r| j                  rdnd d| j                   d| j                   d}d| j                  v rM|j                  d   d   }|j                  dk(  r,|j                  }|d|j                    d|j"                   dz  }g }| j$                  }|"|j'                  d|j)                                 | j*                  }	|	?|	j,                  }
|j'                  d|
j)                          d|	j/                                 | j0                  }|?|j,                  }
|j'                  d|
j)                          d|j/                                 |sdndj3                  |      }d|fd|fd |fgS )!NrV   rW   	untrustedzCertificate subject: "z "
Certificate SHA1 fingerprint: z!
Certificate SHA256 fingerprint: z
Trust anchor: "z"
The signer's certificate is .z#The signature is cryptographically rw   rx   z'sound.

The digest algorithm used was 'z%'.
The signature mechanism used was 'z'.ecdsa	algorithm
parametersnamedz@
The elliptic curve used for the signer's ECDSA public key was 'z' (OID: z).z$Signing time as reported by signer: zSignature timestamp token: z9
The token is guaranteed to be newer than the signature.
zContent timestamp token: z9
The token is guaranteed to be older than the signature.
z0No available information about the signing time.z

zSigner info	IntegrityzSigning time)rH   rV   rW   ri   rj   ry   rz   r{   rl   rE   rF   rJ   rI   
public_keynamechosenr   dottedr   r   	isoformatr   ru   r}   r   ra   )rX   certtrust_statusabout_signervalidity_info	ec_params	curve_oidtiming_infosreported_ts
tst_statusr   content_tst_statustiming_infos                rC   r   z0StandardCMSSignatureStatus.pretty_print_sections[  so   !%!2!2 <<$L\\$L&L%dll&A&A%B C--1YY]]_,= >//3{{/@.A B#112 3++7.	; 	 2[[TZZrT: ;..2.?.?-@ A../r	3 	 d444151M2I ~~(3<3C3C	''0'7'7&8 9&--.b2 --"6{7L7L7N6OP ,,
!%%B-blln-= >L6689;
 "<<)#--B+BLLN+; <L%>>@AC   ?\* 	 L)-([)
 	
rB   )r:   r;   r<   r=   r   r   r   r?   r   r(   r   rq   r>   r   rZ   r   r   r   rp   r   __classcell__r   s   @rC   r.   r.     s    
 .2*1 >B!9:A
 FJ)A BI
 
T 
 
B3"	KE
tE#s(O'< E
rB   r.   c                   &    e Zd ZdZdZ	 dZ	 dZ	 dZy)r/   a"  
    Indicate the extent to which a PDF signature (cryptographically) covers
    a document. Note that this does *not* pass judgment on whether uncovered
    updates are legitimate or not, but as a general rule, a legitimate signature
    will satisfy at least :attr:`ENTIRE_REVISION`.
    r   r#   r   r   N)r:   r;   r<   r=   UNCLEARCONTIGUOUS_BLOCK_FROM_STARTENTIRE_REVISIONENTIRE_FILErA   rB   rC   r/   r/     s8     G
 #$ O KrB   r/   c                   v    e Zd ZU dZee   ed<   	 dZeee	e
f      ed<   	 dZee   ed<   	 edee   fd       Zy)r0   Ncoveragediff_result	docmdp_okr[   c                 @   | j                   }| j                  G|t        j                  k(  ry|t        j                  k(  rt
        j                  S t
        j                  S t        | j                  t              r| j                  j                  S t
        j                  S )z
        Indicates the degree to which the document was modified after the
        signature was applied.

        Will be ``None`` if difference analysis results are not available;
        an instance of :class:`.ModificationLevel` otherwise.
        N)r  r  r/   r  r  r!   NONEOTHER
isinstancer    modification_level)rX   r  s     rC   r!  z#ModificationInfo.modification_level  s     ==#1AAA 5AAA "&& ',,
 ((*5##666$***rB   )r:   r;   r<   r  r   r/   r?   r  r   r    r"   r  r>   rq   r!   r!  rA   rB   rC   r0   r0     sp    15Hh-.5 HLK%
,B BCDK	 !%Ix~$ +H->$? + +rB   r0   c                        e Zd ZU dZdZeed<   	 dZee	   ed<   	 e
def fd       Ze
defd       Z fd	Z fd
Z xZS )r1   z;Class to indicate the validation status of a PDF signature.Fhas_seed_valuesNseed_value_constraint_errorr[   c                 t    t         |   }|xr* | j                  xr | j                  xs | j                  du S )a  
        Formulates a general judgment on the validity of this signature.
        This takes into account the cryptographic validity of the signature,
        the signature's chain of trust, compliance with the document
        modification policy, seed value constraint compliance and the validity
        of the timestamp token (if present).

        :return:
            ``True`` if all constraints are satisfied, ``False`` otherwise.
        N)r   r   seed_value_okr  r!  )rX   generic_checks_okr   s     rC   r   zPdfSignatureStatus.bottom_line  sF     "G/  D""DB4#:#:d#B	
rB   c                     | j                   du S )a  
        Indicates whether the signature satisfies all mandatory constraints in
        the seed value dictionary of the associated form field.

        .. warning::
            Currently, not all seed value entries are recognised by the signer
            and/or the validator, so this judgment may not be entirely accurate
            in some cases.

            See :class:`~.pyhanko.sign.fields.SigSeedValueSpec`.
        N)r$  r]   s    rC   r&  z PdfSignatureStatus.seed_value_ok!  s     //477rB   c              #     K   t         |          E d {    | j                  t        j                  k(  rd nN| j                  t        j
                  k(  r-| j                  d| j                  j                  z    n	d nd | j                  r#| j                  t        j                  k7  rd y y d y 7 w)N	UNTOUCHEDEXTENDED_WITH_EXTENDEDNONSTANDARD_COVERAGEACCEPTABLE_MODIFICATIONSILLEGAL_MODIFICATIONS)	r   rZ   r  r/   r  r  r!  r  r  r   s    rC   rZ   z!PdfSignatureStatus.summary_fields1  s     7)+++==2>>>]]4DDD&&2&)@)@)E)EEE  ((>>}} 6 B BB00 C *) 	,s   C B>B)C c                    t         |          }| j                  t        j                  k(  rd}nh| j
                  Z| j
                  t        j                  k(  rd}n"| j
                  t        j                  k(  rd}nd}d| d| j                  rdnd d	}nd
}|j                  d|f       | j                  r;| j                  rd}nd| j                  j                  z   }|j                  d|f       |S )Nz%The signature covers the entire file.z1All modifications relate to signature maintenancez?All modifications relate to signing and form filling operationsz&Some modifications may be illegitimatez.The signature does not cover the entire file.
z, and they appear to be rw   inz9compatible with the current document modification policy.z'Incremental update analysis was skippedModificationsz4There were no SV issues detected for this signature.zXThe signature did not satisfy the SV constraints on the signature field.
Error message: zSeed value constraints)r   r   r  r/   r  r!  r!   LTA_UPDATESFORM_FILLINGr  r   r#  r&  r$  failure_message)rX   r   modification_strmodlvl_stringsv_infor   s        rC   r   z(PdfSignatureStatus.pretty_print_sectionsB  s   702==2>>>F&&2**.?.K.KKK " ,,0A0N0NN% "
 %MME$o%=!^^r6 7<< ! $M *:;<!!P<66FFG 
 OO5w?@rB   )r:   r;   r<   r=   r#  r>   r?   r$  r   r%   rq   r   r&  rZ   r   r  r  s   @rC   r1   r1     st    E!OT! JN*E!FM
 
T 
 
& 8t 8 8*"& &rB   r1   c                       e Zd ZdZy)r2   zDClass to indicate the validation status of a PDF document timestamp.N)r:   r;   r<   r=   rA   rB   rC   r2   r2   k  s    NrB   r2   )Eloggingcollectionsr   dataclassesr   r   enumr   typingr   r   r	   r
   r   r   r   r   r   r   
asn1cryptor   r   r   r   r   pyhanko_certvalidator.errorsr   r   r   pyhanko_certvalidator.pathr   pyhanko_certvalidator.validater   pdf_utils.miscr   ades.reportr   r   diff_analysisr    r!   r"   errorsr$   r%   settingsr&   __all__	getLoggerr:   r   r3   r'   r(   r)   r*   rp   r   r>   r   r,   r+   r-   r4   r.   r/   r0   r1   r2   rA   rB   rC   <module>rJ     s?    # !     2 1 
 6 = ) 3 
 J )" 
		8	$ $  , $W4 W4 W4t $"
 "
 "
J $    $
. 
 

*}
*),
*3=
*FJ
*8# 8#v0# 0#f $++ ++ ++\ $$ $ $N $\
!6 \
 \
~ ![ ! !H $2+ 2+ 2+j $i)+E i iX $O.0H O OrB   