U
    cH                     @   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	 ddl
mZmZ ddlmZ d	d
dddddddddgZedddg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%dS )&z
.. versionadded:: 0.20.0
    N)	dataclass)datetime	timedelta)	FrozenSetOptional)algoskeys   )PKIXSubtreesRevocationCheckingRuleRevocationCheckingPolicyFreshnessReqTypeCertRevTrustPolicyPKIXValidationParamsAlgorithmUsageConstraintAlgorithmUsagePolicyDisallowWeakAlgorithmsPolicyDEFAULT_WEAK_HASH_ALGOSREQUIRE_REVINFONO_REVOCATIONZmd2md5sha1c                   @   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self r    E/tmp/pip-unpacked-wheel-rwcmptg8/pyhanko_certvalidator/policy_decl.pystrict`   s
    zRevocationCheckingRule.strictc                 C   s   | t jt jfkS r   )r   r   r   r   r    r    r!   toleranti   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_mandatoryp   s    z$RevocationCheckingRule.crl_mandatoryc                 C   s   | t jt jfkS r   )r   r   OCSP_REQUIREDr   r    r    r!   crl_relevantw   s    z#RevocationCheckingRule.crl_relevantc                 C   s   | t jt jfkS r   )r   r'   r%   r   r    r    r!   ocsp_mandatory~   s    z%RevocationCheckingRule.ocsp_mandatoryc                 C   s   | t jt jfkS r   )r   r   r$   r   r    r    r!   ocsp_relevant   s    z$RevocationCheckingRule.ocsp_relevantN)__name__
__module____qualname____doc__r$   r'   r%   CRL_OR_OCSP_REQUIREDr   r   r   propertyboolr"   r#   r&   r(   r)   r*   r    r    r    r!   r   #   s(   T)frozenc                   @   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)clsr5   r    r    r!   from_legacy   s    
z$RevocationCheckingPolicy.from_legacyr   c                 C   s   | j jo| j j S r   )r3   r#   r   r    r    r!   	essential   s    z"RevocationCheckingPolicy.essentialN)r+   r,   r-   r.   r   __annotations__classmethodstrr;   r0   r1   r<   r    r    r    r!   r      s   
)r3   r4   )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=   rD   r   r   r   rC   rE   rF   rG   r1   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)rH   rI   Za_anyZb_anyr    r    r!   intersect_policy_sets  s    
rL   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   rJ   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.
        rJ   )rM   rP   rO   rN   )rM   rP   rO   rN   r   )r   rS   Zinit_policy_setrP   rO   rN   r    r    r!   merge  s&    




zPKIXValidationParams.merge)r+   r,   r-   rK   rM   r=   rN   r1   rO   rP   rQ   r   r
   rR   rT   r    r    r    r!   r   7  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   rU   r   r    r    r!   __bool__  s    z!AlgorithmUsageConstraint.__bool__)r+   r,   r-   r.   r1   r=   rV   r   r   rW   r?   rY   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   r[   r\   r    r    r!   digest_algorithm_allowed  s    z-AlgorithmUsagePolicy.digest_algorithm_allowedr[   r\   
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.
        Nr]   )r   r[   r\   rb   r    r    r!   signature_algorithm_allowed  s    z0AlgorithmUsagePolicy.signature_algorithm_allowedN)r+   r,   r-   r.   r   DigestAlgorithmr   r   r   r`   SignedDigestAlgorithmr   PublicKeyInforc   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   rg   rh   ri   rj   r    r    r!   __init__  s    z%DisallowWeakAlgorithmsPolicy.__init__rZ   c                 C   s   t |d j| jkS )N	algorithm)r   nativerg   r_   r    r    r!   r`     s    z5DisallowWeakAlgorithmsPolicy.digest_algorithm_allowedra   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rsaZdsaFz	Key size z for algorithm z- is considered too small; policy mandates >= )rU   rW   rl   zDigest algorithm z< is not allowed, which disqualifies the signature mechanism z	 as well.)rU   rW   rV   rX   )Zsignature_algorh   
startswithZbit_sizeri   rj   r   	hash_algor9   r`   r   rd   rm   rV   )r   r[   r\   rb   Z	algo_nameZalgo_allowedZis_rsaZis_dsaZkey_szZfailed_thresholdrp   Zdigest_allowedr    r    r!   rc      s@    



 	z8DisallowWeakAlgorithmsPolicy.signature_algorithm_allowedN)r+   r,   r-   r.   r   rK   rk   r   rd   r   r   r   r`   re   r   rf   rc   r    r    r    r!   r     s   
 	)&r.   abcrA   Zdataclassesr   r   r   typingr   r   Z
asn1cryptor   r   Z
name_treesr
   __all__rK   r   uniqueEnumr   r   r/   r   r   r   r   r   r7   r   r   r?   rL   r   r   ABCr   r   r    r    r    r!   <module>   sp   i

. n2