a `L@sdZddlZddlZddlZddlZddlZddlZddlmZddl Z ddl Z ddl Z ddl mZddl mZddlmZddlmZddlmZdd lmZmZeeZGd d d e jZGd d d eee jZGdddeZGdddeZGdddeZe ej!GdddeZ"ej#GdddeZ$ej#Gddde"Z%ej#GdddeZ&ej#Gddde"Z'ej#GdddeZ(ej#Gd d!d!e"Z)ej#Gd"d#d#eZ*ej#Gd$d%d%eZ+dS)&z&ACME Identifier Validation Challenges.N)hashes)SSL)crypto) crypto_util)errors)fields) ResourceMixin TypeMixincs(eZdZdZiZefddZZS) ChallengezACME challenge.c sRztt||WStjyL}z t|t|WYd}~Sd}~00dSN)superr from_jsonjoseZUnrecognizedTypeErrorloggerdebugUnrecognizedChallenge)clsjobjerror __class__1/usr/lib/python3/dist-packages/acme/challenges.pyr s  zChallenge.from_json)__name__ __module__ __qualname____doc__TYPES classmethodr __classcell__rrrrr sr c@s"eZdZdZiZdZeeZdS)ChallengeResponsezACME challenge response.Z challengeN) rrrrrZ resource_typerZResourceresourcerrrrr &sr cs4eZdZdZfddZddZeddZZS)ralUnrecognized 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. cs tt|t|d|dS)Nr)r r__init__object __setattr__selfrrrrr";szUnrecognizedChallenge.__init__cCs|jSr )rr&rrrto_partial_json?sz%UnrecognizedChallenge.to_partial_jsoncCs||Sr r)rrrrrr BszUnrecognizedChallenge.from_json) rrrrr"r(rr rrrrrr.s  rc@s@eZdZdZdZejdejej ej edddZ e ddZ d S) _TokenChallengez3Challenge with token. :ivar bytes token: g0@tokenT)sizeZminimum)encoderdecodercCsd|jvod|jvS)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'rrr good_tokenVs z_TokenChallenge.good_tokenN)rrrrZ TOKEN_SIZErFieldZencode_b64jose functoolspartialZdecode_b64joser*propertyr/rrrrr)Gs r)cs8eZdZdZedZejZ ddZ fddZ Z S)!KeyAuthorizationChallengeResponsez_Response to Challenges based on Key Authorization. :param unicode key_authorization: keyAuthorizationcCs|jd}t|dkr*td|jdS|d|dkrXtd|d|ddSt|j|j d }|d |krtd |d|dSd 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 formedFrr*z8Mismatching token in key authorization: %r instead of %rZ hash_functionz=Mismatching thumbprint in key authorization: %r instead of %rT) key_authorizationsplitlenrrencoder b64encode thumbprintthumbprint_hash_functiondecode)r&challaccount_public_keypartsr?rrrverifyns(    z(KeyAuthorizationChallengeResponse.verifycstt|}|dd|S)Nr5)r r4r(popr%rrrr(s z1KeyAuthorizationChallengeResponse.to_partial_json) rrrrrr0r:rZSHA256r@rEr(rrrrrr4es   r4c@sDeZdZdZeZeZejZddZ ddZ e j ddZ dd Zd 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 cCs&|ddt|j|jdS)z^Generate Key Authorization. :param JWK account_key: :rtype unicode: r*r6r8)r=rr>r?r@rAr& account_keyrrrr:s z+KeyAuthorizationChallenge.key_authorizationcCs|j||dS)zGenerate response to the challenge. :param JWK account_key: :returns: Response (initialized `response_cls`) to the challenge. :rtype: KeyAuthorizationChallengeResponse r:) response_clsr:rHrrrresponses z"KeyAuthorizationChallenge.responsecKs tdS)aGenerate 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)NotImplementedErrorr&rIkwargsrrr validations z$KeyAuthorizationChallenge.validationcOs"|||j|g|Ri|fS)zGenerate response and validation. Convenience function that return results of `response` and `validation`. :param JWK account_key: :rtype: tuple )rLrP)r&rIargsrOrrrresponse_and_validations z1KeyAuthorizationChallenge.response_and_validationN)rrrrNotImplementedtyprKr4r@r:rLabcabstractmethodrPrRrrrrrGs   rGc@seZdZdZdZddZdS) DNS01ResponsezACME dns-01 challenge response.zdns-01cCs|||}|std|S)aSimple 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)rErr)r&rBdomainrCZverifiedrrr simple_verifys  zDNS01Response.simple_verifyN)rrrrrTrZrrrrrWsrWc@s.eZdZdZeZejZdZddZddZ dS)DNS01zACME dns-01 challenge._acme-challengecKs$tt||dS)WGenerate validation. :param JWK account_key: :rtype: unicode utf-8)rr>hashlibsha256r:r=digestrAr&rIZ unused_kwargsrrrrPs  zDNS01.validationcCsd|j|SzkDomain name for TXT validation record. :param unicode name: Domain name being validated. z{0}.{1}formatLABELr&namerrrvalidation_domain_nameszDNS01.validation_domain_nameN) rrrrrWrKrTrfrPrirrrrr[s  r[c@s&eZdZdZdZdZdZdddZdS) HTTP01Responsez ACME http-01 challenge response.zhttp-01Pz Nc Cs|||stddS|durF||jkrFtd||d|7}||}td|j|ztj |dd}Wn8tj j y}zt d||WYd}~dSd}~00td ||j |j|j |j}|j|krtd |j|dSd S) aSimple 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 rXFNz4Using non-standard port for http-01 verification: %sz:{0}zVerifying %s at %s...)rEzUnable to reach %s: %szReceived %s: %s. Headers: %szEKey authorization from response (%r) doesn't match HTTP response (%r)T)rErrPORTZwarningreurirTrequestsget exceptionsZRequestExceptionrtextZheadersrstripWHITESPACE_CUTSETr:) r&rBrYrCportrmZ http_responserZchallenge_responserrrrZs4    zHTTP01Response.simple_verify)N)rrrrrTrlrsrZrrrrrj s rjc@s:eZdZdZeZejZdZeddZ ddZ ddZ d S) HTTP01zACME http-01 challenge.z.well-known/acme-challengecCsd|jd|dS)zTPath (starting with '/') for provisioned resource. :rtype: string /r*) URI_ROOT_PATHr=r'rrrpathSsz HTTP01.pathcCsd||jS)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 zhttp://)rx)r&rYrrrrm\s z HTTP01.uricKs ||S)r]rJrbrrrrPhszHTTP01.validationN) rrrrrjrKrTrwr3rxrmrPrrrrruJs  ruc@sReZdZdZdZdZdZdZeddZ dd d Z dd d Z ddZ dddZ dS)TLSALPN01Responsez$ACME tls-alpn-01 challenge response.z tls-alpn-01is1.3.6.1.5.5.7.1.30.1z acme-tls/1cCst|jdS)z*Hash value stored in challenge certificater^)r_r`r:r=rar'rrrhszTLSALPN01Response.hNcCs\|durt}|tj|dt|jd}tj|jd|d}t j ||gd|gd|fS)aGenerate tls-alpn-01 certificate. :param unicode domain: Domain verified by the challenge. :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` NsDER:hexT)Zcriticalvalue)Z force_san extensions) rZPKeyZ generate_keyZTYPE_RSAcodecsr=rzZ X509ExtensionID_PE_ACME_IDENTIFIER_V1rZ gen_ss_cert)r&rYkeybitsZ der_valueZacme_extensionrrrgen_certs  zTLSALPN01Response.gen_certcCsD|dur t|}td|||dur.|j}tj||||jgdS)zProbe tls-alpn-01 challenge certificate. :param unicode domain: domain being validated, required. :param string host: IP address used to probe the certificate. :param int port: Port used to probe the certificate. Nz%s resolved to %s)hostrtrhZalpn_protocols)socketZ gethostbynamerrrlrZ probe_sniACME_TLS_1_PROTOCOL)r&rYrrtrrr probe_certs  zTLSALPN01Response.probe_certcCst|}td|d|t|dks>|d|krBdSt|D]0}| |}| dkrN| }||j kSqNdS)a Verify tls-alpn-01 challenge certificate. :param unicode domain: Domain name being validated. :param OpensSSL.crypto.X509 cert: Challenge certificate. :returns: Whether the certificate was successfully verified. :rtype: bool zCertificate %s. SANs: %sr`r9rFsUNDEF) rZ _pyopenssl_cert_or_req_all_namesrrrar<lowerrangeZget_extension_countZ get_extensionZget_short_nameget_datarz)r&rYcertnamesiZextdatarrr verify_certs    zTLSALPN01Response.verify_certc Cs~|||stddS|durrz|j|||d}Wn:tjyp}z tjt|ddWYd}~dSd}~00|||S)aSimple verify. Verify ``validation`` using ``account_public_key``, optionally probe tls-alpn-01 certificate and check using `verify_cert`. :param .challenges.TLSALPN01 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 string host: IP address used to probe the certificate. :param int port: Port used to probe the certificate. :returns: ``True`` if and only if client's control of the domain has been verified. :rtype: bool rXFN)rYrrtT)exc_info)rErrrrErrorstrr)r&rBrYrCrrrtrrrrrZs  zTLSALPN01Response.simple_verify)Nr{)NN)NNN)rrrrrTrlrrr3rzrrrrZrrrrryrs   ryc@s.eZdZdZeZejZddZeddZ dS) TLSALPN01zACME tls-alpn-01 challenge.cKs ||j|d|ddS)aGenerate validation. :param JWK account_key: :param unicode domain: Domain verified by the challenge. :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` Zcert_keyrY)rrY)rLrrorNrrrrPs zTLSALPN01.validationcCsttjdottjdS)ai Check if TLS-ALPN-01 challenge is supported on this machine. This implies that a recent version of OpenSSL is installed (>= 1.0.2), or a recent cryptography version shipped with the OpenSSL library is installed. :returns: ``True`` if TLS-ALPN-01 is supported on this machine, ``False`` otherwise. :rtype: bool Zset_alpn_protosZset_alpn_select_callback)hasattrrZ ConnectionZContextrrrr is_supporteds  zTLSALPN01.is_supportedN) rrrrryrKrTrP staticmethodrrrrrrs rc@s>eZdZdZdZdZejfddZddZ dd Z d d Z d S) DNSzACME "dns" challenge.dnsr\cKs(tjjf|jddd||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_keysr^)payloadralg)rJWSZsignZ json_dumpsr=)r&rIrrOrrrgen_validations zDNS.gen_validationc Csb|j|dsdSz|||jdkWStjy\}ztd|WYd}~dSd}~00dS)zwCheck validation. :param JWS validation: :param JWK account_public_key: :rtype: bool )rFr^z&Checking validation for DNS failed: %sN)rEZ json_loadsrrArZDeserializationErrorrr)r&rPrCrrrrcheck_validation)s   zDNS.check_validationcKst|j|fi|dS)zGenerate response. :param .JWK account_key: Private account key. :param .JWA alg: :rtype: DNSResponse )rP) DNSResponserrNrrr gen_response:s zDNS.gen_responsecCsd|j|SrcrdrgrrrriFszDNS.validation_domain_nameN) rrrrrTrfrZRS256rrrrirrrrrs rc@s.eZdZdZdZejdejjdZ ddZ dS)rz@ACME "dns" challenge response. :param JWS validation: rrP)r-cCs||j|S)z~Check validation. :param challenges.DNS chall: :param JWK account_public_key: :rtype: bool )rrP)r&rBrCrrrrZs zDNSResponse.check_validationN) rrrrrTrr0rr rPrrrrrrOsr),rrUrr1r_ZloggingrZcryptography.hazmat.primitivesrZjosepyrrnZsixZOpenSSLrrZacmerrrZ acme.mixinsrr Z getLoggerrrZTypedJSONObjectWithFieldsr r rr)r4Z add_metaclassABCMetarGregisterrWr[rjruryrrrrrrrsP       / @='{$;