o
    &zhxM                     @   s  d Z ddlZddl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g d
Z	 eddZ	 eddG dd dZejG dd dejZeddG dd dZeejejZ	 eejejdZ	 eejejeejejedZ	 ejG dd dejZ eddG dd dZ!dee" dee" dee" fddZ#eddG d d! d!Z$eddG d"d# d#Z%G d$d% d%ej&Z'G d&d' d'e'Z(G d(d) d)e'Z)dS )*z
.. versionadded:: 0.20.0
    N)	dataclass)datetime	timedelta)	FrozenSetIterableOptional)algoskeys   )PKIXSubtrees)RevocationCheckingRuleRevocationCheckingPolicyFreshnessReqTypeCertRevTrustPolicyPKIXValidationParamsAlgorithmUsageConstraintAlgorithmUsagePolicyDisallowWeakAlgorithmsPolicyAcceptAllAlgorithmsNonRevokedStatusAssertionDEFAULT_WEAK_HASH_ALGOSREQUIRE_REVINFONO_REVOCATION)md2md5sha1   )minutesT)frozenc                   @   s$   e Zd ZU dZeed< 	 eed< dS )r   zG
    Assert that a certificate was not revoked at some given date.
    cert_sha256atN)__name__
__module____qualname____doc__bytes__annotations__r    r'   r'   \/var/www/html/kangema/venv/lib/python3.10/site-packages/pyhanko_certvalidator/policy_decl.pyr   -   s   
 r   c                   @   s   e Zd ZdZdZ	 dZ	 dZ	 dZ	 dZ	 dZ		 dZ
	 ed	efd
dZed	efddZed	efddZed	efddZed	efddZed	efddZdS )r   zg
    Rules determining in what circumstances revocation data has to be checked,
    and what kind.
    clrcheck	ocspcheck	bothcheckeitherchecknocheckifdeclaredcheckifdeclaredsoftcheckreturnc                 C   s   | t jt jt jfvS N)r   CHECK_IF_DECLAREDCHECK_IF_DECLARED_SOFTNO_CHECKselfr'   r'   r(   strict{   s
   zRevocationCheckingRule.strictc                 C      | t jt jfv S r1   )r   r3   r4   r5   r'   r'   r(   tolerant      zRevocationCheckingRule.tolerantc                 C   r8   r1   )r   CRL_REQUIREDCRL_AND_OCSP_REQUIREDr5   r'   r'   r(   crl_mandatory   r:   z$RevocationCheckingRule.crl_mandatoryc                 C      | t jt jfvS r1   )r   r4   OCSP_REQUIREDr5   r'   r'   r(   crl_relevant   r:   z#RevocationCheckingRule.crl_relevantc                 C   r8   r1   )r   r?   r<   r5   r'   r'   r(   ocsp_mandatory   r:   z%RevocationCheckingRule.ocsp_mandatoryc                 C   r>   r1   )r   r4   r;   r5   r'   r'   r(   ocsp_relevant   r:   z$RevocationCheckingRule.ocsp_relevantN)r!   r"   r#   r$   r;   r?   r<   CRL_OR_OCSP_REQUIREDr4   r2   r3   propertyboolr7   r9   r=   r@   rA   rB   r'   r'   r'   r(   r   >   s8    r   c                   @   sJ   e Zd ZU dZeed< 	 eed< 	 edefddZe	de
fdd	Zd
S )r   zu
    Class describing a revocation checking policy
    based on the types defined in the ETSI TS 119 172 series.
    ee_certificate_ruleintermediate_ca_cert_rulepolicyc                 C   s*   zt | W S  ty   td| dw )N'z ' is not a valid revocation mode)LEGACY_POLICY_MAPKeyError
ValueError)clsrH   r'   r'   r(   from_legacy   s
   
z$RevocationCheckingPolicy.from_legacyr0   c                 C   s   | j jo| j j S r1   )rF   r9   r5   r'   r'   r(   	essential   s   z"RevocationCheckingPolicy.essentialN)r!   r"   r#   r$   r   r&   classmethodstrrN   rD   rE   rO   r'   r'   r'   r(   r      s   
 r   )rF   rG   )z	soft-failz	hard-failrequirec                   @   s,   e Zd ZdZe Z	 e Z	 e ZdS )r   z%
    Freshness requirement type.
    N)	r!   r"   r#   r$   enumautoDEFAULTMAX_DIFF_REVOCATION_VALIDATIONTIME_AFTER_SIGNATUREr'   r'   r'   r(   r      s    r   c                   @   s\   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< 	 dZeed< dS )	r   zz
    Class describing conditions for trusting revocation info.
    Based on CertificateRevTrust in ETSI TS 119 172-3.
    revocation_checking_policyN	freshnessfreshness_req_type!expected_post_expiry_revinfo_timeFretroactive_revinfo)r!   r"   r#   r$   r   r&   rY   r   r   r   rU   rZ   r[   r\   rE   r'   r'   r'   r(   r     s   
 r   a_polsb_polsr0   c                 C   s:   d| v }d|v }|r|rt dgS |r|S |r|S || @ S )z
    Intersect two sets of policies, taking into account the special
    'any_policy'.

    :param a_pols:
        A set of policies.
    :param b_pols:
        Another set of policies.
    :return:
        The intersection of both.
    
any_policy)	frozenset)r]   r^   a_anyb_anyr'   r'   r(   intersect_policy_sets8  s   
rc   c                   @   sz   e Zd ZU edgZeed< 	 dZeed< 	 dZeed< 	 dZ	eed< 	 dZ
ee ed< 	 dZee ed	< 	 dddZdS )r   r_   user_initial_policy_setFinitial_policy_mapping_inhibitinitial_explicit_policyinitial_any_policy_inhibitNinitial_permitted_subtreesinitial_excluded_subtreesotherr0   c                 C   sd   d| j v r	|j }nd|j v r| j }n|j | j @ }| jo|j}| jo#|j}| jo)|j}t||||dS )aa  
        Combine the conditions of these PKIX validation params with another
        set of parameters, producing the most lenient set of parameters that
        is stricter than both inputs.

        :param other:
            Another set of PKIX validation parameters.
        :return:
            A combined set of PKIX validation parameters.
        r_   )rd   rg   rf   re   )rd   rg   rf   re   r   )r6   rj   init_policy_setrg   rf   re   r'   r'   r(   merge  s&   




zPKIXValidationParams.merge)rj   r   r0   r   )r!   r"   r#   r`   rd   r&   re   rE   rf   rg   rh   r   r   ri   rl   r'   r'   r'   r(   r   T  s   
 	

r   c                   @   sH   e Zd ZU dZeed< 	 dZee ed< 	 dZ	ee
 ed< 	 dd ZdS )r   zh
    Expression of a constraint on the usage of an algorithm (possibly with
    parameter choices).
    allowedNnot_allowed_afterfailure_reasonc                 C   s   | j S r1   rm   r5   r'   r'   r(   __bool__  s   z!AlgorithmUsageConstraint.__bool__)r!   r"   r#   r$   rE   r&   rn   r   r   ro   rQ   rq   r'   r'   r'   r(   r     s   
 r   c                   @   sR   e Zd ZdZdejdee defddZ	dej
dee deej defdd	Zd
S )r   zR
    Abstract interface defining a usage policy for cryptographic algorithms.
    algomomentr0   c                 C      t )a  
        Determine if the indicated digest algorithm can be used at the point
        in time indicated.

        :param algo:
            A digest algorithm description in ASN.1 form.
        :param moment:
            The point in time at which the algorithm should be usable.
            If ``None``, then the returned judgment applies at all times.
        :return:
            A :class:`.AlgorithmUsageConstraint` expressing the judgment.
        NotImplementedErrorr6   rr   rs   r'   r'   r(   digest_algorithm_allowed  s   z-AlgorithmUsagePolicy.digest_algorithm_allowed
public_keyc                 C   rt   )a'  
        Determine if the indicated signature algorithm (including the associated
        digest function and any parameters, if applicable) can be used at the
        point in time indicated.

        :param algo:
            A signature mechanism description in ASN.1 form.
        :param moment:
            The point in time at which the algorithm should be usable.
            If ``None``, then the returned judgment applies at all times.
        :param public_key:
            The public key associated with the operation, if available.

            .. note::
                This parameter can be used to enforce key size limits or
                to filter out keys with known structural weaknesses.
        :return:
            A :class:`.AlgorithmUsageConstraint` expressing the judgment.
        ru   r6   rr   rs   ry   r'   r'   r(   signature_algorithm_allowed  s   z0AlgorithmUsagePolicy.signature_algorithm_allowedN)r!   r"   r#   r$   r   DigestAlgorithmr   r   r   rx   SignedDigestAlgorithmr	   PublicKeyInfor{   r'   r'   r'   r(   r     s$    
r   c                   @   sf   e Zd ZdZee ddfddZdejde	e
 defd	d
Zdejde	e
 de	ej defddZdS )r   a  
    Primitive usage policy that forbids a list of user-specified
    "weak" algorithms and allows everything else.
    It also ignores the time parameter completely.

    .. note::
        This denial-based strategy is supplied to provide a backwards-compatible
        default.
        In many scenarios, an explicit allow-based strategy is more appropriate.
        Users with specific security requirements are encouraged to implement
        :class:`.AlgorithmUsagePolicy` themselves.

    :param weak_hash_algos:
        The list of digest algorithms considered weak.
        Defaults to :const:`.DEFAULT_WEAK_HASH_ALGOS`.
    :param weak_signature_algos:
        The list of digest algorithms considered weak.
        Defaults to the empty set.
    :param rsa_key_size_threshold:
        The key length threshold for RSA keys, in bits.
    :param dsa_key_size_threshold:
        The key length threshold for DSA keys, in bits.
    i   ix  c                 C   s   || _ || _|| _|| _d S r1   )weak_hash_algosweak_signature_algosrsa_key_size_thresholddsa_key_size_threshold)r6   r   r   r   r   r'   r'   r(   __init__)  s   
z%DisallowWeakAlgorithmsPolicy.__init__rr   rs   r0   c                 C   s   t |d j| jvS )N	algorithm)r   nativer   rw   r'   r'   r(   rx   6  s   z5DisallowWeakAlgorithmsPolicy.digest_algorithm_allowedry   c                 C   s  |j }|| jv}|d}|dk}|rH|d urH|s|rH|j}d }	|r+|| jk r+| j}	n
|r5|| jk r5| j}	|	d urHtdd| d| d|	 dS z|j}
W n tyX   d }
Y nw |r|
d ur| 	t
d|ji|}|stdd	| d
|d j d|jdS t|dS )NrsadsaFz	Key size z for algorithm z- is considered too small; policy mandates >= )rm   ro   r   zDigest algorithm z< is not allowed, which disqualifies the signature mechanism z	 as well.)rm   ro   rn   rp   )signature_algor   
startswithbit_sizer   r   r   	hash_algorL   rx   r   r|   r   rn   )r6   rr   rs   ry   	algo_namealgo_allowedis_rsais_dsakey_szfailed_thresholdr   digest_allowedr'   r'   r(   r{   =  sH   



	z8DisallowWeakAlgorithmsPolicy.signature_algorithm_allowedN)r!   r"   r#   r$   r   r`   r   r   r|   r   r   r   rx   r}   r	   r~   r{   r'   r'   r'   r(   r     s.    

r   c                   @   sN   e Zd Zdejdee defddZdej	dee dee
j defddZd	S )
r   rr   rs   r0   c                 C   
   t ddS NTrp   r   rw   r'   r'   r(   rx   m  s   
z,AcceptAllAlgorithms.digest_algorithm_allowedry   c                 C   r   r   r   rz   r'   r'   r(   r{   r  s   
z/AcceptAllAlgorithms.signature_algorithm_allowedN)r!   r"   r#   r   r|   r   r   r   rx   r}   r	   r~   r{   r'   r'   r'   r(   r   l  s"    
r   )*r$   abcrS   dataclassesr   r   r   typingr   r   r   
asn1cryptor   r	   
name_treesr   __all__r`   r   #FRESHNESS_FALLBACK_VALIDITY_DEFAULTr   uniqueEnumr   r   rC   r   r4   r   r3   r2   rJ   r   r   rQ   rc   r   r   ABCr   r   r   r'   r'   r'   r(   <module>   st    
i/
n2\