a oeW@s dZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddlm Z ddlm Z dd lmZdd lmZdd lmZdd lmZddlZdd lmZddlmZddlZddlmZddlmZeeZedddZGdddej Z!Gdddej Z"Gddde!Z#Gddde!Z$Gddde"Z%Gddde$ej&dZ'e"j(Gd d!d!e%Z)e!j(Gd"d#d#e'Z*e"j(Gd$d%d%e%Z+e!j(Gd&d'd'e'Z,e"j(Gd(d)d)e%Z-e!j(Gd*d+d+e'Z.e!j(Gd,d-d-e$Z/e"j(Gd.d/d/e"Z0dS)0z&ACME Identifier Validation Challenges.N)Any)cast)Dict)Mapping)Optional)Tuple)Type)TypeVar)Union)hashes)crypto)SSL) crypto_util)errorsGenericChallenge Challenge)boundcs\eZdZUdZiZeeedfed<e ee e ee fe e dfdfdd ZZS)rzACME challenge.TYPESUnrecognizedChallenge)clsjobjreturnc sTzttt|WStjyN}z t|t|WYd}~Sd}~00dSN) rrsuper from_jsonjoseZUnrecognizedTypeErrorloggerdebugr)rrerror __class__0C:\Program Files\Certbot\pkgs\acme\challenges.pyr%s  zChallenge.from_json)__name__ __module__ __qualname____doc__rrstrr__annotations__ classmethodrrrr r __classcell__r!r!rr"r s csHeZdZUdZiZeeedfed<eee fdfdd Z Z S)ChallengeResponsezACME challenge response.rrcst}||jd|Sr)rto_partial_jsonpopZtype_field_nameselfrrr!r"r-4s z!ChallengeResponse.to_partial_json) r#r$r%r&rrr'rr(rr-r*r!r!rr"r+/s r+csteZdZUdZeeefed<eeefddfdd Z eeefddd Z e eeefddd d Z Z S) 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. rN)rrcstt|d|dS)Nr)r__init__object __setattr__r/rr!r"r1Js zUnrecognizedChallenge.__init__r,cCs|jSr)rr0r!r!r"r-Nsz%UnrecognizedChallenge.to_partial_jsoncCs||Srr!)rrr!r!r"rQszUnrecognizedChallenge.from_json)r#r$r%r&rr'rr(rr1r-r)rr*r!r!rr"r<s  rc@sPeZdZUdZdZejdejej ej edddZ e e d<eeddd Zd S) _TokenChallengez3Challenge with token. :ivar bytes token: g0@tokenT)sizeZminimum)encoderdecoderr,cCsd|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../)r6r4r!r!r" good_tokenes z_TokenChallenge.good_tokenN)r#r$r%r&Z TOKEN_SIZErfieldZencode_b64jose functoolspartialZdecode_b64joser6bytesr(propertyboolr;r!r!r!r"r5Vs  r5cs\eZdZUdZedZeed<e j Z dej e dddZeeefdfd d ZZS) !KeyAuthorizationChallengeResponsez[Response to Challenges based on Key Authorization. :param str key_authorization: keyAuthorizationkey_authorizationKeyAuthorizationChallengechallaccount_public_keyrcCs|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 formedFrr6z8Mismatching token in key authorization: %r instead of %rZ hash_functionz=Mismatching thumbprint in key authorization: %r instead of %rT) rDsplitlenrrencoder b64encode thumbprintthumbprint_hash_functiondecode)r0rGrHpartsrQr!r!r"verify}s(    z(KeyAuthorizationChallengeResponse.verifyr,cst}|dd|S)NrC)rr-r.r/rr!r"r-s  z1KeyAuthorizationChallengeResponse.to_partial_json)r#r$r%r&rr<rDr'r(r ZSHA256rRJWKrArUrrr-r*r!r!rr"rBts  rBc@seZdZUdZeZeed<eZe e ed<e j Z e j edddZe j e dddZeje j eed d d Ze j eeee efd d dZdS)rEzChallenge based on Key Authorization. :param response_cls: Subclass of `KeyAuthorizationChallengeResponse` that will be used to generate ``response``. :param str typ: type of the challenge typ response_cls) account_keyrcCs&|ddt|j|jdS)zZGenerate Key Authorization. :param JWK account_key: :rtype str: r6rIrK)rOrrPrQrRrSr0rYr!r!r"rDs 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 rD)rXrDrZr!r!r"responses z"KeyAuthorizationChallenge.responserYkwargsrcKs 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)NotImplementedErrorr0rYr^r!r!r" validations z$KeyAuthorizationChallenge.validation)rYargsr^rcOs"|||j|g|Ri|fS)zGenerate response and validation. Convenience function that return results of `response` and `validation`. :param JWK account_key: :rtype: tuple )r\ra)r0rYrbr^r!r!r"response_and_validations z1KeyAuthorizationChallenge.response_and_validationN)r#r$r%r&NotImplementedrWr'r(rXrrBrRrrVrDr\abcabstractmethodrrarrcr!r!r!r"rEs     rE) metaclassc@s*eZdZdZdZdeejedddZ dS) DNS01ResponsezACME dns-01 challenge response.zdns-01DNS01)rGdomainrHrcCs|||}|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 str 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)rUrr)r0rGrjrHZverifiedr!r!r" simple_verifys  zDNS01Response.simple_verifyN) r#r$r%r&rWr'rrVrArlr!r!r!r"rhsrhc@sBeZdZdZeZejZdZej e e dddZ e e dddZ d S) rizACME dns-01 challenge._acme-challengerY unused_kwargsrcKs$tt||dS)SGenerate validation. :param JWK account_key: :rtype: str utf-8)rrPhashlibsha256rDrOdigestrSr0rYror!r!r"ra s  zDNS01.validationnamercCs|jd|S)z{Domain name for TXT validation record. :param str name: Domain name being validated. :rtype: str rI)LABELr0rwr!r!r"validation_domain_nameszDNS01.validation_domain_nameN)r#r$r%r&rhrXrWrxrrVrr'rarzr!r!r!r"ris  ric@s<eZdZdZdZdZdZd deej e e e e dd d Z dS) HTTP01Responsez ACME http-01 challenge response.zhttp-01Pz NHTTP01)rGrjrHporttimeoutrc Cs|||stddS|durF||jkrFtd||d|7}||}td|j|ztj |d|d}Wn8tj j y}zt d||WYd}~dSd}~00d |_ td ||j|j|j|j} |j| krtd |j| dSd S) aSimple verify. :param challenges.SimpleHTTP chall: Corresponding challenge. :param str 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. :param int timeout: Timeout in seconds. :returns: ``True`` iff validation with the files currently served by the HTTP server is successful. :rtype: bool rkFNz4Using non-standard port for http-01 verification: %sz:{0}zVerifying %s at %s...)rUrzUnable to reach %s: %sasciizReceived %s: %s. Headers: %szEKey authorization from response (%r) doesn't match HTTP response (%r)T)rUrrPORTZwarningformaturirWrequestsget exceptionsZRequestExceptionrencodingtextZheadersrstripWHITESPACE_CUTSETrD) r0rGrjrHrrrZ http_responserZchallenge_responser!r!r"rl/s6     zHTTP01Response.simple_verify)Nr})r#r$r%r&rWrrr'rrVrintrArlr!r!r!r"r{s  r{c@sTeZdZdZeZejZdZee dddZ e e dddZ e j ee d d d Zd S) r~zACME http-01 challenge.z.well-known/acme-challenger,cCsd|jd|dS)zQPath (starting with '/') for provisioned resource. :rtype: str /r6) URI_ROOT_PATHrOr4r!r!r"pathqsz HTTP01.path)rjrcCsd||jS)zCreate an URI to the provisioned resource. Forms an URI to the HTTPS server provisioned resource (containing :attr:`~SimpleHTTP.token`). :param str domain: Domain name being verified. :rtype: str zhttp://)r)r0rjr!r!r"rzs z HTTP01.urirncKs ||S)rpr[rur!r!r"raszHTTP01.validationN)r#r$r%r&r{rXrWrr@r'rrrrVrrar!r!r!r"r~hs r~c @seZdZdZdZdZdZdZee dddZ de e e jeee je jfd d d Zde e e e ee jdddZe e jedddZdde eje e je e e eedddZd S)TLSALPN01Responsez$ACME tls-alpn-01 challenge response.z tls-alpn-01is1.3.6.1.5.5.7.1.30.1s acme-tls/1r,cCst|jdS)z*Hash value stored in challenge certificaterq)rrrsrDrOrtr4r!r!r"hszTLSALPN01Response.hN)rjkeybitsrcCs\|durt}|tj|dt|jd}tj|jd|d}t j ||gd|gd|fS)aGenerate tls-alpn-01 certificate. :param str 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) r PKeyZ generate_keyZTYPE_RSAcodecsrOrZ X509ExtensionID_PE_ACME_IDENTIFIER_V1rZ gen_ss_cert)r0rjrrZ der_valueZacme_extensionr!r!r"gen_certs  zTLSALPN01Response.gen_cert)rjhostrrcCsL|dur t|}td|||dur.|j}tj||||jgdS)zProbe tls-alpn-01 challenge certificate. :param str domain: domain being validated, required. :param str host: IP address used to probe the certificate. :param int port: Port used to probe the certificate. Nz%s resolved to %s)rrrwZalpn_protocols) socketZ gethostbynamerrrrZ probe_snirOACME_TLS_1_PROTOCOL)r0rjrrr!r!r" probe_certs  zTLSALPN01Response.probe_cert)rjcertrcCst|}td|d|t|dks>|d|krBdSt|D]0}| |}| dkrN| }||j kSqNdS)aVerify tls-alpn-01 challenge certificate. :param str domain: Domain name being validated. :param OpensSSL.crypto.X509 cert: Challenge certificate. :returns: Whether the certificate was successfully verified. :rtype: bool zCertificate %s. SANs: %srsrLrFsUNDEF) rZ _pyopenssl_cert_or_req_all_namesrrrtrNlowerrangeZget_extension_countZ get_extensionZget_short_nameget_datar)r0rjrnamesiextdatar!r!r" verify_certs     zTLSALPN01Response.verify_cert TLSALPN01)rGrjrHrrrrc 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 rkFN)rjrrT)exc_info)rUrrrrErrorr'r)r0rGrjrHrrrrr!r!r"rls  zTLSALPN01Response.simple_verify)Nr)NN)NNN)r#r$r%r&rWrrrr@r?rr'rr rrrX509rrrArrrVrlr!r!r!r"rs,    rc@sLeZdZdZeZejZeje e e j e j fdddZeedddZdS) rzACME tls-alpn-01 challenge.r]cKs,tt||j|dtt|ddS)aGenerate validation. :param JWK account_key: :param str 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_keyrj)rrj)rrr\rrr'r`r!r!r"ras zTLSALPN01.validationr,cCsttjdottjdS)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)hasattrr Z ConnectionZContextr!r!r!r" is_supported(s  zTLSALPN01.is_supportedN)r#r$r%r&rrXrWrrVrrr rrra staticmethodrArr!r!r!r"rs  rc@sreZdZdZdZdZejfejej e ej dddZ ej eje ddd Zeje d d d d ZeedddZdS)DNSzACME "dns" challenge.dnsrm)rYalgr^rcKs(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_keysrq)payloadrr)rJWSsignZ json_dumpsrO)r0rYrr^r!r!r"gen_validation?s zDNS.gen_validation)rarHrc Csb|j|dsdSz|||jdkWStjy\}ztd|WYd}~dSd}~00dS)zwCheck validation. :param JWS validation: :param JWK account_public_key: :rtype: bool )rFrqz&Checking validation for DNS failed: %sN)rUZ json_loadsrrSrZDeserializationErrorrr)r0rarHrr!r!r"check_validationNs   zDNS.check_validation DNSResponser]cKst|j|fi|dS)zGenerate response. :param .JWK account_key: Private account key. :param .JWA alg: :rtype: DNSResponse )ra)rrr`r!r!r" gen_response_s zDNS.gen_responservcCsd|j|S)zgDomain name for TXT validation record. :param str name: Domain name being validated. z{0}.{1})rrxryr!r!r"rzjszDNS.validation_domain_nameN)r#r$r%r&rWrxrZRS256rVZ JWASignaturerrrrArrr'rzr!r!r!r"r7s  rc@sFeZdZUdZdZejdejjdZ eje d<dej e dddZ d S) rz@ACME "dns" challenge response. :param JWS validation: rra)r9rrFcCs||j|S)z~Check validation. :param challenges.DNS chall: :param JWK account_public_key: :rtype: bool )rra)r0rGrHr!r!r"r~s zDNSResponse.check_validationN)r#r$r%r&rWrr<rrrar(rVrArr!r!r!r"rss r)1r&rerr=rrZloggingrtypingrrrrrrrr r Zcryptography.hazmat.primitivesr ZjosepyrZOpenSSLr r rZacmerrZ getLoggerr#rrZTypedJSONObjectWithFieldsrr+rr5rBABCMetarEregisterrhrir{r~rrrrr!r!r!r"s^                 2AH'%;