U
    ;gxM                     @   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 d	d
dddddddddddgZedddg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!ee" ee" ee" d$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_REVOCATIONZ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.
    Zcert_sha256atN)__name__
__module____qualname____doc__bytes__annotations__r    r%   r%   E/tmp/pip-unpacked-wheel-hgp_x7fx/pyhanko_certvalidator/policy_decl.pyr   -   s   
c                   @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
eed	d
dZeed	ddZeed	ddZeed	ddZeed	ddZeed	ddZdS )r   zg
    Rules determining in what circumstances revocation data has to be checked,
    and what kind.
    ZclrcheckZ	ocspcheckZ	bothcheckZeithercheckZnocheckZifdeclaredcheckZifdeclaredsoftcheckreturnc                 C   s   | t jt jt jfkS N)r   CHECK_IF_DECLAREDCHECK_IF_DECLARED_SOFTNO_CHECKselfr%   r%   r&   strict{   s
    zRevocationCheckingRule.strictc                 C   s   | t jt jfkS r)   )r   r+   r,   r-   r%   r%   r&   tolerant   s    zRevocationCheckingRule.tolerantc                 C   s   | t jt jfkS r)   )r   CRL_REQUIREDCRL_AND_OCSP_REQUIREDr-   r%   r%   r&   crl_mandatory   s    z$RevocationCheckingRule.crl_mandatoryc                 C   s   | t jt jfkS r)   )r   r,   OCSP_REQUIREDr-   r%   r%   r&   crl_relevant   s    z#RevocationCheckingRule.crl_relevantc                 C   s   | t jt jfkS r)   )r   r4   r2   r-   r%   r%   r&   ocsp_mandatory   s    z%RevocationCheckingRule.ocsp_mandatoryc                 C   s   | t jt jfkS r)   )r   r,   r1   r-   r%   r%   r&   ocsp_relevant   s    z$RevocationCheckingRule.ocsp_relevantN)r   r    r!   r"   r1   r4   r2   CRL_OR_OCSP_REQUIREDr,   r*   r+   propertyboolr/   r0   r3   r5   r6   r7   r%   r%   r%   r&   r   >   s(   c                   @   sF   e Zd ZU dZeed< eed< eedddZe	e
d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   s4   z
t | W S  tk
r.   td| dY nX d S )N'z ' is not a valid revocation mode)LEGACY_POLICY_MAPKeyError
ValueError)clsr=   r%   r%   r&   from_legacy   s    
z$RevocationCheckingPolicy.from_legacyr'   c                 C   s   | j jo| j j S r)   )r;   r0   r-   r%   r%   r&   	essential   s    z"RevocationCheckingPolicy.essentialN)r   r    r!   r"   r   r$   classmethodstrrC   r9   r:   rD   r%   r%   r%   r&   r      s   
)r;   r<   )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DEFAULTZMAX_DIFF_REVOCATION_VALIDATIONZTIME_AFTER_SIGNATUREr%   r%   r%   r&   r      s
   c                   @   sT   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.
    Zrevocation_checking_policyN	freshnessfreshness_req_type!expected_post_expiry_revinfo_timeFretroactive_revinfo)r   r    r!   r"   r   r$   rK   r   r   r   rJ   rL   rM   rN   r:   r%   r%   r%   r&   r     s   
	)a_polsb_polsr(   c                 C   s>   d| k}d|k}|r"|r"t dgS |r*|S |r2|S || @ S d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_policyN)	frozenset)rO   rP   Za_anyZb_anyr%   r%   r&   intersect_policy_sets8  s    
rS   c                   @   st   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
ddZdS )r   rQ   user_initial_policy_setFinitial_policy_mapping_inhibitinitial_explicit_policyinitial_any_policy_inhibitNinitial_permitted_subtreesinitial_excluded_subtrees)otherr(   c                 C   sd   d| j kr|j }nd|j kr$| j }n|j | j @ }| jo:|j}| joF|j}| joR|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.
        rQ   )rT   rW   rV   rU   )rT   rW   rV   rU   r   )r.   rZ   Zinit_policy_setrW   rV   rU   r%   r%   r&   merge  s&    




zPKIXValidationParams.merge)r   r    r!   rR   rT   r$   rU   r:   rV   rW   rX   r   r   rY   r[   r%   r%   r%   r&   r   T  s   

c                   @   sB   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 r)   r\   r-   r%   r%   r&   __bool__  s    z!AlgorithmUsageConstraint.__bool__)r   r    r!   r"   r:   r$   r]   r   r   r^   rF   r`   r%   r%   r%   r&   r     s
   
c                   @   sH   e Zd ZdZejee edddZ	ej
ee eej edddZdS )	r   zR
    Abstract interface defining a usage policy for cryptographic algorithms.
    algomomentr(   c                 C   s   t dS )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.
        NNotImplementedErrorr.   rb   rc   r%   r%   r&   digest_algorithm_allowed  s    z-AlgorithmUsagePolicy.digest_algorithm_allowedrb   rc   
public_keyr(   c                 C   s   t dS )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.
        Nrd   r.   rb   rc   ri   r%   r%   r&   signature_algorithm_allowed  s    z0AlgorithmUsagePolicy.signature_algorithm_allowedN)r   r    r!   r"   r   DigestAlgorithmr   r   r   rg   SignedDigestAlgorithmr	   PublicKeyInfork   r%   r%   r%   r&   r     s    c                   @   s\   e Zd ZdZee ddfddZeje	e
 edddZeje	e
 e	ej ed	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 r)   )weak_hash_algosweak_signature_algosrsa_key_size_thresholddsa_key_size_threshold)r.   ro   rp   rq   rr   r%   r%   r&   __init__)  s    z%DisallowWeakAlgorithmsPolicy.__init__ra   c                 C   s   t |d j| jkS )N	algorithm)r   nativero   rf   r%   r%   r&   rg   6  s    z5DisallowWeakAlgorithmsPolicy.digest_algorithm_allowedrh   c                 C   s  |j }|| jk}|d}|dk}|r|d k	r|s6|r|j}d }	|rV|| jk rV| j}	n|rj|| jk rj| j}	|	d k	rtdd| d| d|	 dS z
|j}
W n tk
r   d }
Y nX |r|
d k	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 >= )r\   r^   rt   zDigest algorithm z< is not allowed, which disqualifies the signature mechanism z	 as well.)r\   r^   r]   r_   )Zsignature_algorp   
startswithZbit_sizerq   rr   r   	hash_algorA   rg   r   rl   ru   r]   )r.   rb   rc   ri   Z	algo_nameZalgo_allowedZis_rsaZis_dsaZkey_szZfailed_thresholdry   Zdigest_allowedr%   r%   r&   rk   =  s@    



 	z8DisallowWeakAlgorithmsPolicy.signature_algorithm_allowedN)r   r    r!   r"   r   rR   rs   r   rl   r   r   r   rg   rm   r	   rn   rk   r%   r%   r%   r&   r     s   
 	c                   @   sD   e Zd Zejee edddZej	ee ee
j edddZdS )r   ra   c                 C   s
   t ddS NTr_   r   rf   r%   r%   r&   rg   m  s    z,AcceptAllAlgorithms.digest_algorithm_allowedrh   c                 C   s
   t ddS rz   r{   rj   r%   r%   r&   rk   r  s    z/AcceptAllAlgorithms.signature_algorithm_allowedN)r   r    r!   r   rl   r   r   r   rg   rm   r	   rn   rk   r%   r%   r%   r&   r   l  s    )*r"   abcrH   dataclassesr   r   r   typingr   r   r   Z
asn1cryptor   r	   Z
name_treesr   __all__rR   r   Z#FRESHNESS_FALLBACK_VALIDITY_DEFAULTr   uniqueEnumr   r   r8   r   r,   r   r+   r*   r?   r   r   rF   rS   r   r   ABCr   r   r   r%   r%   r%   r&   <module>   s|   
i

0 n2\