U
    [^8                     @   s  d Z ddlZddlZddlZddlZddlmZ ddlZddl	Z	ddl
Z
ddlmZ eeZG dd dejZG dd dejZG d	d
 d
eZG dd deZG dd deZe
ejG dd deZejG dd deZejG dd deZejG dd deZejG dd deZejG dd deZejG dd deZejG dd deZ ejG dd  d eZ!dS )!z&ACME Identifier Validation Challenges.    N)hashes)fieldsc                       s(   e Zd ZdZi Ze fddZ  ZS )	ChallengezACME challenge.c              
      sT   zt t| |W S  tjk
rN } zt| t| W Y S d }~X Y nX d S N)superr   	from_jsonjoseZUnrecognizedTypeErrorloggerdebugUnrecognizedChallenge)clsjobjerror	__class__ 1/usr/lib/python3/dist-packages/acme/challenges.pyr      s
    
zChallenge.from_json)__name__
__module____qualname____doc__TYPESclassmethodr   __classcell__r   r   r   r   r      s   r   c                   @   s"   e Zd ZdZi ZdZeeZdS )ChallengeResponsezACME challenge response.Z	challengeN)	r   r   r   r   r   Zresource_typer   ZResourceresourcer   r   r   r   r      s   r   c                       s4   e Zd ZdZ fddZdd Zedd Z  ZS )r   al  Unrecognized challenge.

    ACME specification defines a generic framework for challenges and
    defines some standard challenges that are implemented in this
    module. However, other implementations (including peers) might
    define additional challenge types, which should be ignored if
    unrecognized.

    :ivar jobj: Original JSON decoded object.

    c                    s    t t|   t| d| d S )Nr   )r   r   __init__object__setattr__selfr   r   r   r   r   4   s    zUnrecognizedChallenge.__init__c                 C   s   | j S r   )r   r    r   r   r   to_partial_json8   s    z%UnrecognizedChallenge.to_partial_jsonc                 C   s   | |S r   r   )r   r   r   r   r   r   ;   s    zUnrecognizedChallenge.from_json)	r   r   r   r   r   r"   r   r   r   r   r   r   r   r   '   s
   r   c                   @   s@   e Zd ZdZdZejdejej	ej
edddZedd Zd	S )
_TokenChallengez3Challenge with token.

    :ivar bytes token:

    g      0@tokenT)sizeZminimum)encoderdecoderc                 C   s   d| j kod| j kS )zIs `token` good?

        .. todo:: acme-spec wants "It MUST NOT contain any non-ASCII
           characters", but it should also warrant that it doesn't
           contain ".." or "/"...

        s   ..   /)r$   r!   r   r   r   
good_tokenO   s    z_TokenChallenge.good_tokenN)r   r   r   r   Z
TOKEN_SIZEr   FieldZencode_b64jose	functoolspartialZdecode_b64joser$   propertyr)   r   r   r   r   r#   @   s       r#   c                       s8   e Zd ZdZedZejZ	dd Z
 fddZ  ZS )!KeyAuthorizationChallengeResponsez_Response to Challenges based on Key Authorization.

    :param unicode key_authorization:

    keyAuthorizationc                 C   s   | j d}t|dkr*td| j  dS |d |dkrXtd|d |d dS t|j| j	d
 }|d	 |krtd
|d | dS dS )a%  Verify the key authorization.

        :param KeyAuthorization chall: Challenge that corresponds to
            this response.
        :param JWK account_public_key:

        :return: ``True`` iff verification of the key authorization was
            successful.
        :rtype: bool

        .   z)Key authorization (%r) is not well formedFr   r$   z8Mismatching token in key authorization: %r instead of %rZhash_function   z=Mismatching thumbprint in key authorization: %r instead of %rT)key_authorizationsplitlenr	   r
   encoder   	b64encode
thumbprintthumbprint_hash_functiondecode)r    challaccount_public_keypartsr9   r   r   r   verifyg   s,      z(KeyAuthorizationChallengeResponse.verifyc                    s   t t|  }|dd  |S )Nr/   )r   r.   r"   popr   r   r   r   r"      s    z1KeyAuthorizationChallengeResponse.to_partial_json)r   r   r   r   r   r*   r4   r   ZSHA256r:   r?   r"   r   r   r   r   r   r.   ^   s
   
 r.   c                   @   sD   e Zd ZdZeZeZejZdd Z	dd Z
ejdd Zdd	 Zd
S )KeyAuthorizationChallengezChallenge based on Key Authorization.

    :param response_cls: Subclass of `KeyAuthorizationChallengeResponse`
        that will be used to generate `response`.
    :param str typ: type of the challenge
    c                 C   s&   |  dd t|j| jd  S )z^Generate Key Authorization.

        :param JWK account_key:
        :rtype unicode:

        r$   r0   r2   )r7   r   r8   r9   r:   r;   r    account_keyr   r   r   r4      s
    z+KeyAuthorizationChallenge.key_authorizationc                 C   s   | j | |dS )zGenerate response to the challenge.

        :param JWK account_key:

        :returns: Response (initialized `response_cls`) to the challenge.
        :rtype: KeyAuthorizationChallengeResponse

        r4   )response_clsr4   rB   r   r   r   response   s    	z"KeyAuthorizationChallenge.responsec                 K   s
   t  dS )a  Generate validation for the challenge.

        Subclasses must implement this method, but they are likely to
        return completely different data structures, depending on what's
        necessary to complete the challenge. Interpretation of that
        return value must be known to the caller.

        :param JWK account_key:
        :returns: Challenge-specific validation.

        NNotImplementedErrorr    rC   kwargsr   r   r   
validation   s    z$KeyAuthorizationChallenge.validationc                 O   s   |  || j|f||fS )zGenerate response and validation.

        Convenience function that return results of `response` and
        `validation`.

        :param JWK account_key:
        :rtype: tuple

        )rF   rK   )r    rC   argsrJ   r   r   r   response_and_validation   s    
z1KeyAuthorizationChallenge.response_and_validationN)r   r   r   r   NotImplementedtyprE   r.   r:   r4   rF   abcabstractmethodrK   rM   r   r   r   r   rA      s   
rA   c                   @   s   e Zd ZdZdZdd ZdS )DNS01ResponsezACME dns-01 challenge response.zdns-01c                 C   s   |  ||}|std |S )a  Simple verify.

        This method no longer checks DNS records and is a simple wrapper
        around `KeyAuthorizationChallengeResponse.verify`.

        :param challenges.DNS01 chall: Corresponding challenge.
        :param unicode domain: Domain name being verified.
        :param JWK account_public_key: Public key for the key pair
            being authorized.

        :return: ``True`` iff verification of the key authorization was
            successful.
        :rtype: bool

        4Verification of key authorization in response failed)r?   r	   r
   )r    r<   domainr=   Zverifiedr   r   r   simple_verify   s    
zDNS01Response.simple_verifyN)r   r   r   r   rO   rU   r   r   r   r   rR      s   rR   c                   @   s.   e Zd ZdZeZejZdZdd Zdd Z	dS )DNS01zACME dns-01 challenge._acme-challengec                 K   s$   t t| |d  S )WGenerate validation.

        :param JWK account_key:
        :rtype: unicode

        utf-8)r   r8   hashlibZsha256r4   r7   Zdigestr;   r    rC   Zunused_kwargsr   r   r   rK      s
    zDNS01.validationc                 C   s   d | j|S zkDomain name for TXT validation record.

        :param unicode name: Domain name being validated.

        z{0}.{1}formatLABELr    namer   r   r   validation_domain_name   s    zDNS01.validation_domain_nameN)
r   r   r   r   rR   rE   rO   r_   rK   rb   r   r   r   r   rV      s   
rV   c                   @   s&   e Zd ZdZdZdZdZdddZdS )	HTTP01Responsez ACME http-01 challenge response.zhttp-01P   z
	 Nc           	   
   C   s   |  ||std dS |dk	rF|| jkrFtd| |d|7 }||}td|j| zt	|}W n: tj
jk
r } ztd|| W Y dS d}~X Y nX td||j|j |j| j}| j|krtd	| j| dS d
S )a  Simple verify.

        :param challenges.SimpleHTTP chall: Corresponding challenge.
        :param unicode domain: Domain name being verified.
        :param JWK account_public_key: Public key for the key pair
            being authorized.
        :param int port: Port used in the validation.

        :returns: ``True`` iff validation with the files currently served by the
            HTTP server is successful.
        :rtype: bool

        rS   FNz4Using non-standard port for http-01 verification: %sz:{0}zVerifying %s at %s...zUnable to reach %s: %szReceived %s: %s. Headers: %szEKey authorization from response (%r) doesn't match HTTP response (%r)T)r?   r	   r
   PORTZwarningr^   urirO   requestsget
exceptionsZRequestExceptionr   textZheadersrstripWHITESPACE_CUTSETr4   )	r    r<   rT   r=   Zportrf   Zhttp_responser   Zchallenge_responser   r   r   rU     s8    
 
 
zHTTP01Response.simple_verify)N)r   r   r   r   rO   re   rl   rU   r   r   r   r   rc     s
   rc   c                   @   s:   e Zd ZdZeZejZdZedd Z	dd Z
dd Zd	S )
HTTP01zACME http-01 challenge.z.well-known/acme-challengec                 C   s   d| j  d | d S )zTPath (starting with '/') for provisioned resource.

        :rtype: string

        /r$   )URI_ROOT_PATHr7   r!   r   r   r   pathL  s    zHTTP01.pathc                 C   s   d| | j  S )zCreate an URI to the provisioned resource.

        Forms an URI to the HTTPS server provisioned resource
        (containing :attr:`~SimpleHTTP.token`).

        :param unicode domain: Domain name being verified.
        :rtype: string

        zhttp://)rp   )r    rT   r   r   r   rf   U  s    
z
HTTP01.uric                 K   s
   |  |S )rX   rD   r[   r   r   r   rK   a  s    zHTTP01.validationN)r   r   r   r   rc   rE   rO   ro   r-   rp   rf   rK   r   r   r   r   rm   C  s   
rm   c                   @   s   e Zd ZdZdZdS )TLSALPN01Responsea  ACME TLS-ALPN-01 challenge response.

    This class only allows initiating a TLS-ALPN-01 challenge returned from the
    CA. Full support for responding to TLS-ALPN-01 challenges by generating and
    serving the expected response certificate is not currently provided.
    tls-alpn-01N)r   r   r   r   rO   r   r   r   r   rq   k  s   rq   c                   @   s    e Zd ZdZdZeZdd ZdS )	TLSALPN01zACME tls-alpn-01 challenge.

    This class simply allows parsing the TLS-ALPN-01 challenge returned from
    the CA. Full TLS-ALPN-01 support is not currently provided.

    rr   c                 K   s
   t  dS )z&Generate validation for the challenge.NrG   rI   r   r   r   rK     s    zTLSALPN01.validationN)r   r   r   r   rO   rq   rE   rK   r   r   r   r   rs   v  s   rs   c                   @   s>   e Zd ZdZdZdZejfddZdd Z	dd	 Z
d
d ZdS )DNSzACME "dns" challenge.dnsrW   c                 K   s(   t jjf | jddd||d|S )zGenerate validation.

        :param .JWK account_key: Private account key.
        :param .JWA alg:

        :returns: This challenge wrapped in `.JWS`
        :rtype: .JWS

        T)Z	sort_keysrY   )payloadkeyalg)r   JWSZsignZ
json_dumpsr7   )r    rC   rx   rJ   r   r   r   gen_validation  s    
 zDNS.gen_validationc              
   C   sd   |j |dsdS z| | |jdkW S  tjk
r^ } ztd| W Y dS d}~X Y nX dS )zwCheck validation.

        :param JWS validation:
        :param JWK account_public_key:
        :rtype: bool

        )rw   FrY   z&Checking validation for DNS failed: %sN)r?   Z
json_loadsrv   r;   r   ZDeserializationErrorr	   r
   )r    rK   r=   r   r   r   r   check_validation  s    
zDNS.check_validationc                 K   s   t | j|f|dS )zGenerate response.

        :param .JWK account_key: Private account key.
        :param .JWA alg:

        :rtype: DNSResponse

        )rK   )DNSResponserz   rI   r   r   r   gen_response  s
    	zDNS.gen_responsec                 C   s   d | j|S r\   r]   r`   r   r   r   rb     s    zDNS.validation_domain_nameN)r   r   r   r   rO   r_   r   ZRS256rz   r{   r}   rb   r   r   r   r   rt     s   rt   c                   @   s.   e Zd ZdZdZejdejjdZ	dd Z
dS )r|   z@ACME "dns" challenge response.

    :param JWS validation:

    ru   rK   )r'   c                 C   s   | | j|S )z~Check validation.

        :param challenges.DNS chall:
        :param JWK account_public_key:

        :rtype: bool

        )r{   rK   )r    r<   r=   r   r   r   r{     s    	zDNSResponse.check_validationN)r   r   r   r   rO   r   r*   ry   r   rK   r{   r   r   r   r   r|     s   r|   )"r   rP   r+   rZ   ZloggingZcryptography.hazmat.primitivesr   Zjosepyr   rg   ZsixZacmer   Z	getLoggerr   r	   ZTypedJSONObjectWithFieldsr   r   r   r#   r.   Zadd_metaclassABCMetarA   registerrR   rV   rc   rm   rq   rs   rt   r|   r   r   r   r   <module>   sB   
/
@='
;