3 گaT@s$dZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddlm Z ddlm Z dd lmZdd lmZddlZdd lmZdd lmZddlZdd lmZddlmZddlmZddlmZddlmZejeZ Gdddej!Z"Gdddeeej!Z#Gddde"Z$Gddde"Z%Gddde#Z&Gddde%ej'dZ(e#j)Gdd 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%Z0e#j)Gd-d.d.e#Z1dS)/z&ACME Identifier Validation Challenges.N)cast)Any)Dict)Mapping)Optional)Tuple)Type)hashes)crypto)SSL) crypto_util)errors)fields) ResourceMixin) TypeMixincsJeZdZUdZiZeeedfee ee fddfdd Z Z S) ChallengezACME challenge.)jobjreturncsDy tj|Stjk r>}ztj|tj|Sd}~XnXdS)N)super from_jsonjoseZUnrecognizedTypeErrorloggerdebugUnrecognizedChallenge)clsrerror) __class__ /usr/lib/python3.6/challenges.pyr$s   zChallenge.from_json) __name__ __module__ __qualname____doc__TYPESrstrr classmethodrrr __classcell__rr)rrrs rc@s4eZdZUdZiZeeedfdZe j eZ dS)ChallengeResponsezACME challenge response.Z challengeN) rr r!r"r#rr$rZ resource_typerZResourceZresourcerrrrr'-s r'csbeZdZdZeeefddfdd ZeeefdddZ 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. N)rrcstjtj|d|dS)Nr)r__init__object __setattr__)selfr)rrrr(Bs zUnrecognizedChallenge.__init__)rcCs|jS)N)r)r+rrrto_partial_jsonFsz%UnrecognizedChallenge.to_partial_jsoncCs||S)Nr)rrrrrrIszUnrecognizedChallenge.from_json) rr r!r"rr$rr(rr,r%rr&rr)rrr5s  rc@sLeZdZUdZd Zejdejej ej edddZ e  e edd d Zd S) _TokenChallengez3Challenge with token. :ivar bytes token: tokenT)sizeZminimum)encoderdecoder)rcCsd|jkod|jkS)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../)r0)r+rrr good_token]s z_TokenChallenge.good_tokenNg0@)rr r!r"Z TOKEN_SIZErFieldZencode_b64jose functoolspartialZdecode_b64joser0bytespropertyboolr5rrrrr-Ns  r-csReZdZdZejdZejZ dej e dddZ e eefdfdd ZZS) !KeyAuthorizationChallengeResponsez_Response to Challenges based on Key Authorization. :param unicode key_authorization: keyAuthorizationKeyAuthorizationChallenge)challaccount_public_keyrcCs|jjd}t|dkr*tjd|jdS|d|jdkrXtjd|d|jddStj|j|j dj }|d |krtjd |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 formedFrr0z8Mismatching token in key authorization: %r instead of %r) hash_functionz=Mismatching thumbprint in key authorization: %r instead of %rT) key_authorizationsplitlenrrencoder b64encode thumbprintthumbprint_hash_functiondecode)r+r?r@partsrJrrrverifyus    z(KeyAuthorizationChallengeResponse.verify)rcstj}|jdd|S)Nr=)rr,pop)r+r)rrrr,s  z1KeyAuthorizationChallengeResponse.to_partial_json)rr r!r"rr6rEr ZSHA256rKJWKr;rNrr$rr,r&rr)rrr<ls   r<c@seZdZUdZeZeeZee e j Z e j edddZ e j e dddZeje j eeddd Ze j eeee efd d d Zd S)r>zChallenge based on Key Authorization. :param response_cls: Subclass of `KeyAuthorizationChallengeResponse` that will be used to generate ``response``. :param str typ: type of the challenge ) account_keyrcCs&|jddtj|j|jdjS)z^Generate Key Authorization. :param JWK account_key: :rtype unicode: r0rA)rC)rHrrIrJrKrL)r+rQrrrrEsz+KeyAuthorizationChallenge.key_authorizationcCs|j|j|dS)zGenerate response to the challenge. :param JWK account_key: :returns: Response (initialized `response_cls`) to the challenge. :rtype: KeyAuthorizationChallengeResponse )rE) response_clsrE)r+rQrrrresponses z"KeyAuthorizationChallenge.response)rQkwargsrcKs 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)NotImplementedError)r+rQrTrrr validations z$KeyAuthorizationChallenge.validation)rQargsrTrcOs|j||j|f||fS)zGenerate response and validation. Convenience function that return results of `response` and `validation`. :param JWK account_key: :rtype: tuple )rSrV)r+rQrWrTrrrresponse_and_validations z1KeyAuthorizationChallenge.response_and_validationN)rr r!r"NotImplementedtypr$rRrr<rKrrPrErSabcabstractmethodrrVrrXrrrrr>s    r>) metaclassc@s*eZdZdZdZdeejedddZ dS) DNS01ResponsezACME dns-01 challenge response.zdns-01DNS01)r?domainr@rcCs|j||}|stjd|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 z4Verification of key authorization in response failed)rNrr)r+r?r`r@Zverifiedrrr simple_verifys  zDNS01Response.simple_verifyN) rr r!r"rZr$rrPr;rarrrrr^sr^c@sBeZdZdZeZejZdZej e e dddZ e e dddZ d S) r_zACME dns-01 challenge.z_acme-challenge)rQ unused_kwargsrcKs$tjtj|j|jdjjS)zWGenerate validation. :param JWK account_key: :rtype: unicode zutf-8)rrIhashlibsha256rErHdigestrL)r+rQrbrrrrVs zDNS01.validation)namercCsdj|j|S)zkDomain name for TXT validation record. :param unicode name: Domain name being validated. z{0}.{1})formatLABEL)r+rfrrrvalidation_domain_name szDNS01.validation_domain_nameN)rr r!r"r^rRrZrhrrPrr$rVrirrrrr_s  r_c@s:eZdZdZdZdZdZd deej e e e ddd Z dS) HTTP01Responsez ACME http-01 challenge response.zhttp-01Pz NHTTP01)r?r`r@portrc Cs|j||stjddS|dk rF||jkrFtjd||dj|7}|j|}tjd|j|ytj |dd}Wn2tj j k r}ztj d||dSd}~XnXd |_ tjd ||j|j|jj|j}|j|krtjd |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 z4Verification of key authorization in response failedFNz4Using non-standard port for http-01 verification: %sz:{0}zVerifying %s at %s...)rNzUnable to reach %s: %sasciizReceived %s: %s. Headers: %szEKey authorization from response (%r) doesn't match HTTP response (%r)T)rNrrPORTZwarningrgurirZrequestsget exceptionsZRequestExceptionrencodingtextZheadersrstripWHITESPACE_CUTSETrE) r+r?r`r@rmrpZ http_responserZchallenge_responserrrra#s0      zHTTP01Response.simple_verify)N)rr r!r"rZrorwr$rrPrintr;rarrrrrjs rjc@sTeZdZdZeZejZdZee dddZ e e dddZ e j ee d d d Zd S) rlzACME http-01 challenge.z.well-known/acme-challenge)rcCsd|jd|jdS)zTPath (starting with '/') for provisioned resource. :rtype: string /r0) URI_ROOT_PATHrH)r+rrrpathdsz HTTP01.path)r`rcCsd||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://)r{)r+r`rrrrpms z HTTP01.uri)rQrbrcKs |j|S)zWGenerate validation. :param JWK account_key: :rtype: unicode )rE)r+rQrbrrrrVyszHTTP01.validationN)rr r!r"rjrRrZrzr:r$r{rprrPrrVrrrrrl[s rlc @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.1z acme-tls/1)rcCstj|jjdjS)z*Hash value stored in challenge certificatezutf-8)rcrdrErHre)r+rrrhszTLSALPN01Response.hN)r`keybitsrcCs\|dkrtj}|jtj|dtj|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) r PKeyZ generate_keyZTYPE_RSAcodecsrHr}Z X509ExtensionID_PE_ACME_IDENTIFIER_V1r Z gen_ss_cert)r+r`rrZ der_valueZacme_extensionrrrgen_certs   zTLSALPN01Response.gen_cert)r`hostrmrcCsL|dkr tj|}tjd|||dkr.|j}tj|j||j|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)rrmrfZalpn_protocols) socketZ gethostbynamerrror Z probe_snirHACME_TLS_1_PROTOCOL)r+r`rrmrrr probe_certs  zTLSALPN01Response.probe_cert)r`certrcCstj|}tjd|jd|t|dks>|dj|jkrBdSx= 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 ConnectionZContextrrrr is_supporteds zTLSALPN01.is_supportedN)rr r!r"r|rRrZrrPrrr rrrV staticmethodr;rrrrrrs  rc@sreZdZdZdZdZejfejej e ej dddZ ej eje ddd Zeje d d d d ZeedddZdS)DNSzACME "dns" challenge.dnsz_acme-challenge)rQalgrTrcKs(tjjf|jddjd||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_keyszutf-8)payloadrr)rJWSZsignZ json_dumpsrH)r+rQrrTrrrgen_validation1s zDNS.gen_validation)rVr@rcCsZ|j|dsdSy||j|jjdkStjk rT}ztjd|dSd}~XnXdS)zwCheck validation. :param JWS validation: :param JWK account_public_key: :rtype: bool )rFzutf-8z&Checking validation for DNS failed: %sN)rNZ json_loadsrrLrZDeserializationErrorrr)r+rVr@rrrrcheck_validation@s  zDNS.check_validation DNSResponse)rQrTrcKst|j|f|dS)zGenerate response. :param .JWK account_key: Private account key. :param .JWA alg: :rtype: DNSResponse )rV)rr)r+rQrTrrr gen_responseQs zDNS.gen_response)rfrcCsdj|j|S)zkDomain name for TXT validation record. :param unicode name: Domain name being validated. z{0}.{1})rgrh)r+rfrrrri]szDNS.validation_domain_nameN)rr r!r"rZrhrZRS256rPZ JWASignaturerrrr;rrr$rirrrrr)s rc@s:eZdZdZdZejdejjdZ dej e dddZ d S) rz@ACME "dns" challenge response. :param JWS validation: rrV)r3r)r?r@rcCs|jttj|j|S)z~Check validation. :param challenges.DNS chall: :param JWK account_public_key: :rtype: bool )rrrrrV)r+r?r@rrrrqs zDNSResponse.check_validationN) rr r!r"rZrr6rrrVrPr;rrrrrrfsr)2r"r[rr7rcZloggingrZtypingrrrrrrrZcryptography.hazmat.primitivesr ZjosepyrZOpenSSLr r rqZacmer r rZ acme.mixinsrrZ getLoggerrrZTypedJSONObjectWithFieldsrr'rr-r<ABCMetar>registerr^r_rjrlr|rrrrrrrs^                /AG'$<