ó
®è­]c           @   s£  d  Z  d d l Z d d l 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 Z d d l m Z d d l m Z d d l m Z e j e ƒ Z d e
 j f d „  ƒ  YZ d	 e
 j f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e j e j ƒ d e f d „  ƒ  Yƒ Z e j d e f d „  ƒ  Yƒ Z e j d e f d „  ƒ  Yƒ Z  e j d e f d „  ƒ  Yƒ Z! e j d e f d „  ƒ  Yƒ Z" e j d e f d „  ƒ  Yƒ Z# e j d e f d „  ƒ  Yƒ Z$ e j d e f d  „  ƒ  Yƒ Z% e j d! e f d" „  ƒ  Yƒ Z& e j d# e f d$ „  ƒ  Yƒ Z' e j d% e f d& „  ƒ  Yƒ Z( d S('   s&   ACME Identifier Validation Challenges.iÿÿÿÿN(   t   hashes(   t   errors(   t   crypto_util(   t   fieldst	   Challengec           B   s#   e  Z d  Z i  Z e d „  ƒ Z RS(   s   ACME challenge.c         C   sN   y t  t |  ƒ j | ƒ SWn- t j k
 rI } t j | ƒ t j | ƒ SXd  S(   N(   t   superR   t	   from_jsont   joset   UnrecognizedTypeErrort   loggert   debugt   UnrecognizedChallenge(   t   clst   jobjt   error(    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR      s
    (   t   __name__t
   __module__t   __doc__t   TYPESt   classmethodR   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR      s   t   ChallengeResponsec           B   s)   e  Z d  Z i  Z d Z e j e ƒ Z RS(   s   ACME challenge response.t	   challenge(   R   R   R   R   t   resource_typeR   t   Resourcet   resource(    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR   '   s   R   c           B   s/   e  Z d  Z d „  Z d „  Z e d „  ƒ Z RS(   sl  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         C   s*   t  t |  ƒ j ƒ  t j |  d | ƒ d  S(   NR   (   R   R   t   __init__t   objectt   __setattr__(   t   selfR   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR   <   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   to_partial_json@   s    c         C   s
   |  | ƒ S(   N(    (   R   R   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR   D   s    (   R   R   R   R   R   R   R   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR   /   s   		t   _TokenChallengec           B   s]   e  Z d  Z d d Z e j d d e j d e j e j	 d e d e
 ƒƒZ e d „  ƒ Z RS(	   s3   Challenge with token.

    :ivar bytes token:

    i€   i   t   tokent   encodert   decodert   sizet   minimumc         C   s   d |  j  k o d |  j  k S(   sÅ   Is `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   ..t   /(   R   (   R   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt
   good_tokenX   s    (   R   R   R   t
   TOKEN_SIZER   t   Fieldt   encode_b64joset	   functoolst   partialt   decode_b64joset   TrueR   t   propertyR%   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR   I   s   
t!   KeyAuthorizationChallengeResponsec           B   sJ   e  Z d  Z e j d ƒ Z e j Z d „  Z	 d „  Z
 d „  Z d „  Z RS(   s_   Response to Challenges based on Key Authorization.

    :param unicode key_authorization:

    t   keyAuthorizationc         O   s*   t  t |  ƒ j | | Ž  |  j t ƒ d  S(   N(   R   R.   R   t   _dump_authorization_keyt   False(   R   t   argst   kwargs(    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR   o   s    c         C   sË   |  j  j d ƒ } t | ƒ d k r; t j d |  j  ƒ t S| d | j d ƒ k rx t j d | d | j d ƒ ƒ t St j | j	 d |  j
 ƒ ƒ j ƒ  } | d | k rÇ t j d	 | d | ƒ t St S(
   s%  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

        t   .i   s)   Key authorization (%r) is not well formedi    R   s8   Mismatching token in key authorization: %r instead of %rt   hash_functioni   s=   Mismatching thumbprint in key authorization: %r instead of %r(   t   key_authorizationt   splitt   lenR	   R
   R1   t   encodeR   t	   b64encodet
   thumbprintt   thumbprint_hash_functiont   decodeR,   (   R   t   challt   account_public_keyt   partsR;   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   verifys   s     	
		c         C   s   t  j |  d | ƒ d S(   s  
        Set if keyAuthorization is dumped in the JSON representation of this ChallengeResponse.
        NB: This method is declared as private because it will eventually be removed.
        :param bool dump: True to dump the keyAuthorization, False otherwise
        t   _dump_auth_keyN(   R   R   (   R   t   dump(    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR0   “   s    c         C   s5   t  t |  ƒ j ƒ  } |  j s1 | j d d  ƒ n  | S(   NR/   (   R   R.   R   RB   t   popt   None(   R   R   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR   œ   s    	(   R   R   R   R   R'   R6   R    t   SHA256R<   R   RA   R0   R   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR.   f   s   			 		t   KeyAuthorizationChallengec           B   sP   e  Z d  Z e Z e Z e j Z d „  Z d „  Z	 e
 j d „  ƒ Z d „  Z RS(   sÐ   Challenge 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   s3   |  j  d ƒ d t j | j d |  j ƒ ƒ j ƒ  S(   s^   Generate Key Authorization.

        :param JWK account_key:
        :rtype unicode:

        R   R4   R5   (   R9   R   R:   R;   R<   R=   (   R   t   account_key(    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR6   ²   s    	c         C   s   |  j  d |  j | ƒ ƒ S(   sË   Generate response to the challenge.

        :param JWK account_key:

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

        R6   (   t   response_clsR6   (   R   RH   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   response½   s    		c         K   s   t  ƒ  ‚ d S(   sˆ  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(   t   NotImplementedError(   R   RH   R3   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt
   validationÉ   s    c         O   s"   |  j  | ƒ |  j | | | Ž f S(   s¼   Generate response and validation.

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

        :param JWK account_key:
        :rtype: tuple

        (   RJ   RL   (   R   RH   R2   R3   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   response_and_validationØ   s    
(   R   R   R   t   NotImplementedt   typRI   R.   R<   R6   RJ   t   abct   abstractmethodRL   RM   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyRG   ¤   s   			t   DNS01Responsec           B   s   e  Z d  Z d Z d „  Z RS(   s   ACME dns-01 challenge response.s   dns-01c         C   s,   |  j  | | ƒ } | s( t j d ƒ n  | S(   sð  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

        s4   Verification of key authorization in response failed(   RA   R	   R
   (   R   R>   t   domainR?   t   verified(    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   simple_verifyë   s    (   R   R   R   RO   RU   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyRR   æ   s   t   DNS01c           B   s5   e  Z d  Z e Z e j Z d Z d „  Z d „  Z RS(   s   ACME dns-01 challenge.s   _acme-challengec         K   s4   t  j t j |  j | ƒ j d ƒ ƒ j ƒ  ƒ j ƒ  S(   sW   Generate validation.

        :param JWK account_key:
        :rtype: unicode

        s   utf-8(   R   R:   t   hashlibt   sha256R6   R9   t   digestR=   (   R   RH   t   unused_kwargs(    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyRL     s    c         C   s   d j  |  j | ƒ S(   sk   Domain name for TXT validation record.

        :param unicode name: Domain name being validated.

        s   {0}.{1}(   t   formatt   LABEL(   R   t   name(    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   validation_domain_name  s    (	   R   R   R   RR   RI   RO   R\   RL   R^   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyRV     s   		
t   HTTP01Responsec           B   s,   e  Z d  Z d Z d Z d Z d d „ Z RS(   s    ACME http-01 challenge response.s   http-01iP   s   
	 c   	      C   s*  |  j  | | ƒ s# t j d ƒ t S| d k	 rd | |  j k rd t j d | ƒ | d j | ƒ 7} n  | j | ƒ } t j d | j	 | ƒ y t
 j | ƒ } Wn- t
 j j k
 rË } t j d | | ƒ t SXt j d | | j | j ƒ | j j |  j ƒ } |  j | k r&t j d |  j | ƒ t St S(	   s¾  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

        s4   Verification of key authorization in response faileds4   Using non-standard port for http-01 verification: %ss   :{0}s   Verifying %s at %s...s   Unable to reach %s: %ss   Received %s: %s. Headers: %ssE   Key authorization from response (%r) doesn't match HTTP response (%r)N(   RA   R	   R
   R1   RE   t   PORTt   warningR[   t   uriRO   t   requestst   gett
   exceptionst   RequestExceptionR   t   textt   headerst   rstript   WHITESPACE_CUTSETR6   R,   (	   R   R>   RS   R?   t   portRb   t   http_responseR   t   challenge_response(    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyRU   .  s.    
	N(   R   R   R   RO   R`   Rj   RE   RU   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR_     s
   t   HTTP01c           B   sD   e  Z d  Z e Z e j Z d Z e d „  ƒ Z d „  Z	 d „  Z
 RS(   s   ACME http-01 challenge.s   .well-known/acme-challengec         C   s   d |  j  d |  j d ƒ S(   sT   Path (starting with '/') for provisioned resource.

        :rtype: string

        R$   R   (   t   URI_ROOT_PATHR9   (   R   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   pathe  s    c         C   s   d | |  j  S(   sö   Create 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

        s   http://(   Rp   (   R   RS   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyRb   n  s    
c         K   s   |  j  | ƒ S(   sW   Generate validation.

        :param JWK account_key:
        :rtype: unicode

        (   R6   (   R   RH   RZ   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyRL   z  s    (   R   R   R   R_   RI   RO   Ro   R-   Rp   Rb   RL   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyRn   \  s   			t   TLSSNI01Responsec           B   sk   e  Z d  Z d Z d Z d Z e d „  ƒ Z e d „  ƒ Z d d d „ Z
 d „  Z d	 „  Z d d
 „ Z RS(   s#   ACME tls-sni-01 challenge response.s
   tls-sni-01s   .acme.invalidi»  c         C   s+   t  j |  j j d ƒ ƒ j ƒ  j ƒ  j ƒ  S(   sC   ``z`` value used for verification.

        :rtype bytes:

        s   utf-8(   RW   RX   R6   R9   t	   hexdigestt   lower(   R   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   z”  s    c         C   s!   |  j  d  d |  j  d |  j S(   sW   Domain name used for verification, generated from `z`.

        :rtype bytes:

        i    R4   (   Rt   t   DOMAIN_SUFFIX(   R   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   z_domainž  s    i   c         C   s_   | d k r4 t j j ƒ  } | j t j j | ƒ n  t j | d |  j j	 ƒ  g d t
 ƒ| f S(   sk  Generate tls-sni-01 certificate.

        :param OpenSSL.crypto.PKey key: Optional private key used in
            certificate generation. If not provided (``None``), then
            fresh key will be generated.
        :param int bits: Number of bits for newly generated key.

        :rtype: `tuple` of `OpenSSL.crypto.X509` and `OpenSSL.crypto.PKey`

        t   dummyt	   force_sanN(   RE   t   OpenSSLt   cryptot   PKeyt   generate_keyt   TYPE_RSAR   t   gen_ss_certRv   R=   R,   (   R   t   keyt   bits(    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   gen_cert§  s
    	c         K   sh   d | k r; t  j | ƒ } t j d | | ƒ | | d <n  | j d |  j ƒ |  j | d <t j |   S(   sQ   Probe tls-sni-01 challenge certificate.

        :param unicode domain:

        t   hosts   %s resolved to %sRk   R]   (	   t   sockett   gethostbynameR	   R
   t
   setdefaultR`   Rv   R   t	   probe_sni(   R   RS   R3   R‚   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt
   probe_cert¹  s    c         C   s>   t  j | ƒ } t j d | j d ƒ | ƒ |  j j ƒ  | k S(   sÏ   Verify tls-sni-01 challenge certificate.

        :param OpensSSL.crypto.X509 cert: Challenge certificate.

        :returns: Whether the certificate was successfully verified.
        :rtype: bool

        s   Certificate %s. SANs: %sRX   (   R   t   _pyopenssl_cert_or_req_sanR	   R
   RY   Rv   R=   (   R   t   certt   sans(    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   verify_certË  s    
c         K   s‹   |  j  | | ƒ s# t j d ƒ t S| d k r~ y |  j d | |  } Wq~ t j k
 rz } t j t | ƒ d t	 ƒt SXn  |  j
 | ƒ S(   s  Simple verify.

        Verify ``validation`` using ``account_public_key``, optionally
        probe tls-sni-01 certificate and check using `verify_cert`.

        :param .challenges.TLSSNI01 chall: Corresponding challenge.
        :param str domain: Domain name being validated.
        :param JWK account_public_key:
        :param OpenSSL.crypto.X509 cert: Optional certificate. If not
            provided (``None``) certificate will be retrieved using
            `probe_cert`.
        :param int port: Port used to probe the certificate.


        :returns: ``True`` iff client's control of the domain has been
            verified.
        :rtype: bool

        s4   Verification of key authorization in response failedRS   t   exc_infoN(   RA   R	   R
   R1   RE   R‡   R   t   Errort   strR,   R‹   (   R   R>   RS   R?   R‰   R3   R   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyRU   Ù  s    N(   R   R   R   RO   Ru   R`   R-   Rt   Rv   RE   R   R‡   R‹   RU   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyRq   „  s   
			t   TLSSNI01c           B   s/   e  Z d  Z e Z e j Z d „  Z d „  Z RS(   s   ACME tls-sni-01 challenge.c         O   s3   t  j d t d d ƒt t |  ƒ j | | Ž  d  S(   Ns5   TLS-SNI-01 is deprecated, and will stop working soon.t
   stackleveli   (   t   warningst   warnt   DeprecationWarningR   R   R   (   R   R2   R3   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR     s    	c         K   s"   |  j  | ƒ j d | j d ƒ ƒ S(   sC  Generate validation.

        :param JWK account_key:
        :param OpenSSL.crypto.PKey cert_key: Optional private key used
            in certificate generation. If not provided (``None``), then
            fresh key will be generated.

        :rtype: `tuple` of `OpenSSL.crypto.X509` and `OpenSSL.crypto.PKey`

        R   t   cert_key(   RJ   R   Rd   (   R   RH   R3   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyRL   
  s    (   R   R   R   Rq   RI   RO   R   RL   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR   ü  s
   		t   TLSALPN01Responsec           B   s   e  Z d  Z d Z RS(   s  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.
    s   tls-alpn-01(   R   R   R   RO   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR•     s   t	   TLSALPN01c           B   s#   e  Z d  Z d Z e Z d „  Z RS(   s¯   ACME 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.

    s   tls-alpn-01c         K   s   t  ƒ  ‚ d S(   s&   Generate validation for the challenge.N(   RK   (   R   RH   R3   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyRL   .  s    (   R   R   R   RO   R•   RI   RL   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR–   #  s   t   DNSc           B   sD   e  Z d  Z d Z d Z e j d „ Z d „  Z d „  Z	 d „  Z
 RS(   s   ACME "dns" challenge.t   dnss   _acme-challengec         K   s7   t  j j d |  j d t ƒ j d ƒ d | d | |  S(   s·   Generate validation.

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

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

        t   payloadt	   sort_keyss   utf-8R   t   alg(   R   t   JWSt   signt
   json_dumpsR,   R9   (   R   RH   R›   R3   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   gen_validation;  s    
c         C   sg   | j  d | ƒ s t Sy# |  |  j | j j d ƒ ƒ k SWn' t j k
 rb } t j d | ƒ t SXd S(   sw   Check validation.

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

        R   s   utf-8s&   Checking validation for DNS failed: %sN(	   RA   R1   t
   json_loadsR™   R=   R   t   DeserializationErrorR	   R
   (   R   RL   R?   R   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   check_validationI  s    	c         K   s   t  d |  j | |  ƒ S(   s‰   Generate response.

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

        :rtype: DNSResponse

        RL   (   t   DNSResponseRŸ   (   R   RH   R3   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   gen_responseZ  s    	c         C   s   d j  |  j | ƒ S(   sk   Domain name for TXT validation record.

        :param unicode name: Domain name being validated.

        s   {0}.{1}(   R[   R\   (   R   R]   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR^   f  s    (   R   R   R   RO   R\   R   t   RS256RŸ   R¢   R¤   R^   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR—   3  s   		R£   c           B   s8   e  Z d  Z d Z e j d d e j j ƒZ d „  Z	 RS(   s@   ACME "dns" challenge response.

    :param JWS validation:

    R˜   RL   R!   c         C   s   | j  |  j | ƒ S(   s~   Check validation.

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

        :rtype: bool

        (   R¢   RL   (   R   R>   R?   (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR¢   z  s    	(
   R   R   R   RO   R   R'   Rœ   R   RL   R¢   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyR£   o  s   ()   R   RP   R)   RW   t   loggingRƒ   R‘   t   cryptography.hazmat.primitivesR    t   josepyR   Ry   Rc   t   sixt   acmeR   R   R   t	   getLoggerR   R	   t   TypedJSONObjectWithFieldsR   R   R   R   R.   t   add_metaclasst   ABCMetaRG   t   registerRR   RV   R_   Rn   Rq   R   R•   R–   R—   R£   (    (    (    s3   /usr/lib/python2.7/dist-packages/acme/challenges.pyt   <module>   sT   >A			=	'	w		
		;	