a }|g.X@s,dZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddlm Z ddlm Z dd lmZdd lmZdd lmZdd lmZdd lmZddlZddlmZddlmZddlZddlmZddlmZeeZ edddZ!Gdddej"Z#Gdddej"Z$Gddde#Z%Gddde#Z&Gddde$Z'Gddde&ej(d 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&Z1e$j*Gd/d0d0e$Z2dS)1z&ACME Identifier Validation Challenges.N)Any)cast)Dict)Mapping)Optional)Tuple)Type)TypeVar)Union)x509)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__3/usr/lib/python3.9/site-packages/acme/challenges.pyr&s  zChallenge.from_json)__name__ __module__ __qualname____doc__rrstrr__annotations__ classmethodrrrr r __classcell__r"r"r r#r!s csHeZdZUdZiZeeedfed<eee fdfdd Z Z S)ChallengeResponsezACME challenge response.rrcst}||jd|Sr)rto_partial_jsonpopZtype_field_nameselfrr r"r#r.5s z!ChallengeResponse.to_partial_json) r$r%r&r'rrr(rr)rr.r+r"r"r r#r,0s 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__r0r r"r#r2Ks zUnrecognizedChallenge.__init__r-cCs|jSr)rr1r"r"r#r.Osz%UnrecognizedChallenge.to_partial_jsoncCs||Srr")rrr"r"r#rRszUnrecognizedChallenge.from_json)r$r%r&r'rr(rr)rr2r.r*rr+r"r"r r#r=s  rc@sPeZdZUdZdZejdejej ej edddZ e e d<eeddd Zd S) _TokenChallengez3Challenge with token. :ivar bytes token: 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../)r8r5r"r"r# good_tokenfs z_TokenChallenge.good_tokenN)r$r%r&r'Z TOKEN_SIZErfieldZencode_b64jose functoolspartialZdecode_b64joser8bytesr)propertyboolr=r"r"r"r#r6Ws  r6cs\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 formedFrr8z8Mismatching token in key authorization: %r instead of %rZ hash_functionz=Mismatching thumbprint in key authorization: %r instead of %rT) rFsplitlenrrencoder b64encode thumbprintthumbprint_hash_functiondecode)r1rIrJpartsrSr"r"r#verify~s(    z(KeyAuthorizationChallengeResponse.verifyr-cst}|dd|S)NrE)rr.r/r0r r"r#r.s  z1KeyAuthorizationChallengeResponse.to_partial_json)r$r%r&r'rr>rFr(r)r SHA256rTJWKrCrWrrr.r+r"r"r r#rDus  rDc@seZdZUdZeZeed<eZe e ed<e j Z e j edddZe j e dddZeje j eed d d Ze j eeee efd d dZdS)rGzChallenge 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: r8rKrM)rQrrRrSrTrUr1r\r"r"r#rFs 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 rF)r[rFr]r"r"r#responses z"KeyAuthorizationChallenge.responser\kwargsrcKs 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)NotImplementedErrorr1r\rar"r"r# validations z$KeyAuthorizationChallenge.validation)r\argsrarcOs"|||j|g|Ri|fS)zGenerate response and validation. Convenience function that return results of `response` and `validation`. :param JWK account_key: :rtype: tuple )r_rd)r1r\rerar"r"r#response_and_validations z1KeyAuthorizationChallenge.response_and_validationN)r$r%r&r'NotImplementedrZr(r)r[rrDrTrrYrFr_abcabstractmethodrrdrrfr"r"r"r#rGs     rG) metaclassc@s*eZdZdZdZdeejedddZ dS) DNS01ResponsezACME dns-01 challenge response.zdns-01DNS01)rIdomainrJrcCs|||}|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)rWrr)r1rIrmrJZverifiedr"r"r# simple_verifys  zDNS01Response.simple_verifyN) r$r%r&r'rZr(rrYrCror"r"r"r#rksrkc@sBeZdZdZeZejZdZej e e dddZ e e dddZ d S) rlzACME dns-01 challenge._acme-challenger\ unused_kwargsrcKs$tt||dS)SGenerate validation. :param JWK account_key: :rtype: str utf-8)rrRhashlibsha256rFrQdigestrUr1r\rrr"r"r#rd s  zDNS01.validationnamercCs|jd|S)z{Domain name for TXT validation record. :param str name: Domain name being validated. :rtype: str rK)LABELr1rzr"r"r#validation_domain_nameszDNS01.validation_domain_nameN)r$r%r&r'rkr[rZr{rrYrr(rdr}r"r"r"r#rls  rlc@s<eZdZdZdZdZdZd deej e e e e dd d Z dS) HTTP01Responsez ACME http-01 challenge response.zhttp-01Pz NHTTP01)rIrmrJporttimeoutrc 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 rnFNz4Using non-standard port for http-01 verification: %sz:{0}zVerifying %s at %s...)rWrzUnable to reach %s: %sasciizReceived %s: %s. Headers: %szEKey authorization from response (%r) doesn't match HTTP response (%r)T)rWrrPORTZwarningformaturirZrequestsget exceptionsZRequestExceptionrencodingtextZheadersrstripWHITESPACE_CUTSETrF) r1rIrmrJrrrZ http_responserZchallenge_responser"r"r#ro0s6     zHTTP01Response.simple_verify)Nr)r$r%r&r'rZrrr(rrYrintrCror"r"r"r#r~ s  r~c@sTeZdZdZeZejZdZee dddZ e e dddZ e j ee d d d Zd S) rzACME http-01 challenge.z.well-known/acme-challenger-cCsd|jd|dS)zQPath (starting with '/') for provisioned resource. :rtype: str /r8) URI_ROOT_PATHrQr5r"r"r#pathrsz HTTP01.path)rmrcCsd||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)r1rmr"r"r#r{s z HTTP01.urirqcKs ||S)rsr^rxr"r"r#rdszHTTP01.validationN)r$r%r&r'r~r[rZrrBr(rrrrYrrdr"r"r"r#ris rc @seZdZdZdZdZdZdZee dddZ de e e jeee je jfd d d Zde e e e ee jdddZe eeje jfedddZdde 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 certificatert)rurvrFrQrwr5r"r"r#hszTLSALPN01Response.hN)rmkeybitsrcCs\|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_RSAcodecsrQrZ X509ExtensionID_PE_ACME_IDENTIFIER_V1rZ gen_ss_cert)r1rmrrZ der_valueZacme_extensionr"r"r#gen_certs  zTLSALPN01Response.gen_cert)rmhostrrcCsL|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)rrrzZalpn_protocols) socketZ gethostbynamerrrrZ probe_snirQACME_TLS_1_PROTOCOL)r1rmrrr"r"r# probe_certs  zTLSALPN01Response.probe_cert)rmcertrcCst|tjs|}t|j|j}t d| t |t |dks\|d|kr`dSz|jt|j}WntjyYdS0|jj|jkS)a@Verify tls-alpn-01 challenge certificate. :param str domain: Domain name being validated. :param cert: Challenge certificate. :type cert: `cryptography.x509.Certificate` or `OpenSSL.crypto.X509` :returns: Whether the certificate was successfully verified. :rtype: bool zCertificate %s. SANs: %srNrF) isinstancer CertificateZto_cryptographyrZ%get_names_from_subject_and_extensionsZsubjectrrrZ fingerprintr rXrPlowerZget_extension_for_oidZObjectIdentifierrrUZExtensionNotFoundrr)r1rmrnamesextr"r"r# verify_certs"  zTLSALPN01Response.verify_cert TLSALPN01)rIrmrJrrrrc 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 rnFN)rmrrT)exc_info)rWrrrrErrorr(r)r1rIrmrJrrrrr"r"r#ros  zTLSALPN01Response.simple_verify)Nr)NN)NNN)r$r%r&r'rZrrrrBrArr(rr rrrX509rrr r rrCrrrYror"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_keyrm)rrm)rrr_rrr(rcr"r"r#rds 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)hasattrrZ ConnectionZContextr"r"r"r# is_supported.s  zTLSALPN01.is_supportedN)r$r%r&r'rr[rZrrYrrr rrrd staticmethodrCrr"r"r"r#rs  rc@sreZdZdZdZdZejfejej e ej dddZ ej eje ddd Zeje d d d d ZeedddZdS)DNSzACME "dns" challenge.dnsrp)r\algrarcKs(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_keysrt)payloadrr)rJWSsignZ json_dumpsrQ)r1r\rrar"r"r#gen_validationEs zDNS.gen_validation)rdrJrc Csb|j|dsdSz|||jdkWStjy\}ztd|WYd}~dSd}~00dS)zwCheck validation. :param JWS validation: :param JWK account_public_key: :rtype: bool )rFrtz&Checking validation for DNS failed: %sN)rWZ json_loadsrrUrZDeserializationErrorrr)r1rdrJrr"r"r#check_validationTs   zDNS.check_validation DNSResponser`cKst|j|fi|dS)zGenerate response. :param .JWK account_key: Private account key. :param .JWA alg: :rtype: DNSResponse )rd)rrrcr"r"r# gen_responsees zDNS.gen_responserycCsd|j|S)zgDomain name for TXT validation record. :param str name: Domain name being validated. z{0}.{1})rr{r|r"r"r#r}pszDNS.validation_domain_nameN)r$r%r&r'rZr{rZRS256rYZ JWASignaturerrrrCrrr(r}r"r"r"r#r=s  rc@sFeZdZUdZdZejdejjdZ eje d<dej e dddZ d S) rz@ACME "dns" challenge response. :param JWS validation: rrd)r;rrHcCs||j|S)z~Check validation. :param challenges.DNS chall: :param JWK account_public_key: :rtype: bool )rrd)r1rIrJr"r"r#rs zDNSResponse.check_validationN)r$r%r&r'rZrr>rrrdr)rYrCrr"r"r"r#rys r)3r'rhrr?ruZloggingrtypingrrrrrrrr r Z cryptographyr Zcryptography.hazmat.primitivesr ZjosepyrZOpenSSLr rrZacmerrZ getLoggerr$rrZTypedJSONObjectWithFieldsrr,rr6rDABCMetarGregisterrkrlr~rrrrrr"r"r"r#s`                  2AH'%;