def determine_authenticator(all_auths, config): """Returns a valid IAuthenticator. :param list all_auths: Where each is a :class:`letsencrypt.client.interfaces.IAuthenticator` object :param config: Used if an authenticator was specified on the command line. :type config: :class:`letsencrypt.client.interfaces.IConfig` :returns: Valid Authenticator object or None :raises letsencrypt.client.errors.LetsEncryptClientError: If no authenticator is available. """ # Available Authenticator objects avail_auths = {} # Error messages for misconfigured authenticators errs = {} for auth_name, auth in all_auths.iteritems(): try: auth.prepare() except errors.LetsEncryptMisconfigurationError as err: errs[auth] = err except errors.LetsEncryptNoInstallationError: continue avail_auths[auth_name] = auth # If an authenticator was specified on the command line, try to use it if config.authenticator: try: auth = avail_auths[config.authenticator] except KeyError: logging.info(list_available_authenticators(avail_auths)) raise errors.LetsEncryptClientError( "The specified authenticator '%s' could not be found" % config.authenticator) elif len(avail_auths) > 1: auth = display_ops.choose_authenticator(avail_auths.values(), errs) elif len(avail_auths.keys()) == 1: auth = avail_auths[avail_auths.keys()[0]] else: raise errors.LetsEncryptClientError("No Authenticators available.") if auth is not None and auth in errs: logging.error( "Please fix the configuration for the Authenticator. " "The following error message was received: " "%s", errs[auth]) return return auth
def obtain_certificate(self, domains, csr=None): """Obtains a certificate from the ACME server. :meth:`.register` must be called before :meth:`.obtain_certificate` .. todo:: This function does not currently handle csr correctly... :param set domains: domains to get a certificate :param csr: CSR must contain requested domains, the key used to generate this CSR can be different than self.authkey :type csr: :class:`CSR` :returns: cert_key, cert_path, chain_path :rtype: `tuple` of (:class:`letsencrypt.client.le_util.Key`, str, str) """ if self.auth_handler is None: msg = ("Unable to obtain certificate because authenticator is " "not set.") logging.warning(msg) raise errors.LetsEncryptClientError(msg) if self.account.regr is None: raise errors.LetsEncryptClientError( "Please register with the ACME server first.") # Perform Challenges/Get Authorizations authzr = self.auth_handler.get_authorizations(domains) # Create CSR from names cert_key = crypto_util.init_save_key( self.config.rsa_key_size, self.config.key_dir) csr = crypto_util.init_save_csr( cert_key, domains, self.config.cert_dir) # Retrieve certificate certr = self.network.request_issuance( jose.ComparableX509( M2Crypto.X509.load_request_der_string(csr.data)), authzr) # Save Certificate cert_path, chain_path = self.save_certificate( certr, self.config.cert_path, self.config.chain_path) revoker.Revoker.store_cert_key( cert_path, self.account.key.file, self.config) return cert_key, cert_path, chain_path
def _from_config_fp(cls, config, config_fp): try: acc_config = configobj.ConfigObj(infile=config_fp, file_error=True, create_empty=False) except IOError: raise errors.LetsEncryptClientError( "Account for %s does not exist" % os.path.basename(config_fp)) if os.path.basename(config_fp) != "default": email = os.path.basename(config_fp) else: email = None phone = acc_config["phone"] if acc_config["phone"] != "None" else None with open(acc_config["key"]) as key_file: key = le_util.Key(acc_config["key"], key_file.read()) if "RegistrationResource" in acc_config: acc_config_rr = acc_config["RegistrationResource"] regr = messages2.RegistrationResource( uri=acc_config_rr["uri"], new_authzr_uri=acc_config_rr["new_authzr_uri"], terms_of_service=acc_config_rr["terms_of_service"], body=messages2.Registration.from_json(acc_config_rr["body"])) else: regr = None return cls(config, key, email, phone, regr)
def make_or_verify_dir(directory, mode=0o755, uid=0): """Make sure directory exists with proper permissions. :param str directory: Path to a directory. :param int mode: Directory mode. :param int uid: Directory owner. :raises LetsEncryptClientError: if a directory already exists, but has wrong permissions or owner :raises OSError: if invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. """ try: os.makedirs(directory, mode) except OSError as exception: if exception.errno == errno.EEXIST: if not check_permissions(directory, mode, uid): raise errors.LetsEncryptClientError( "%s exists, but does not have the proper " "permissions or owner" % directory) else: raise
def _construct_dv_chall(self, chall, domain): """Construct Auth Type Challenges. :param dict chall: Single challenge :param str domain: challenge's domain :returns: challenge_util named tuple Chall object :rtype: `collections.namedtuple` :raises errors.LetsEncryptClientError: If unimplemented challenge exists """ if chall["type"] == "dvsni": logging.info(" DVSNI challenge for name %s.", domain) return challenge_util.DvsniChall( domain, str(chall["r"]), str(chall["nonce"]), self.authkey[domain]) elif chall["type"] == "simpleHttps": logging.info(" SimpleHTTPS challenge for name %s.", domain) return challenge_util.SimpleHttpsChall( domain, str(chall["token"]), self.authkey[domain]) elif chall["type"] == "dns": logging.info(" DNS challenge for name %s.", domain) return challenge_util.DnsChall(domain, str(chall["token"])) else: raise errors.LetsEncryptClientError( "Unimplemented Auth Challenge: %s" % chall["type"])
def send(self, msg): """Send ACME message to server. :param msg: ACME message. :type msg: :class:`letsencrypt.acme.messages.Message` :returns: Server response message. :rtype: :class:`letsencrypt.acme.messages.Message` :raises letsencrypt.acme.errors.ValidationError: if `msg` is not valid serializable ACME JSON message. :raises errors.LetsEncryptClientError: in case of connection error or if response from server is not a valid ACME message. """ try: response = requests.post( self.server_url, data=msg.json_dumps(), headers={"Content-Type": "application/json"}, verify=True) except requests.exceptions.RequestException as error: raise errors.LetsEncryptClientError( 'Sending ACME message to server has failed: %s' % error) json_string = response.json() try: return messages.Message.from_json(json_string) except jose.DeserializationError as error: logging.error(json_string) raise # TODO
def deploy_certificate(self, domains, privkey, cert_file, chain_file=None): """Install certificate :param list domains: list of domains to install the certificate :param privkey: private key for certificate :type privkey: :class:`letsencrypt.client.le_util.Key` :param str cert_file: certificate file path :param str chain_file: chain file path """ if self.installer is None: logging.warning("No installer specified, client is unable to deploy" "the certificate") raise errors.LetsEncryptClientError("No installer available") chain = None if chain_file is None else os.path.abspath(chain_file) for dom in domains: self.installer.deploy_cert(dom, os.path.abspath(cert_file), os.path.abspath(privkey.file), chain) self.installer.save("Deployed Let's Encrypt Certificate") # sites may have been enabled / final cleanup self.installer.restart() display_ops.success_installation(domains)
def enhance_config(self, domains, redirect=None): """Enhance the configuration. .. todo:: This needs to handle the specific enhancements offered by the installer. We will also have to find a method to pass in the chosen values efficiently. :param list domains: list of domains to configure :param redirect: If traffic should be forwarded from HTTP to HTTPS. :type redirect: bool or None :raises letsencrypt.client.errors.LetsEncryptClientError: if no installer is specified in the client. """ if self.installer is None: logging.warning("No installer is specified, there isn't any " "configuration to enhance.") raise errors.LetsEncryptClientError("No installer available") if redirect is None: redirect = enhancements.ask("redirect") if redirect: self.redirect_to_ssl(domains)
def _construct_client_chall(self, chall, domain): # pylint: disable=no-self-use """Construct Client Type Challenges. :param dict chall: Single challenge :param str domain: challenge's domain :returns: challenge_util named tuple Chall object :rtype: `collections.namedtuple` :raises errors.LetsEncryptClientError: If unimplemented challenge exists """ if chall["type"] == "recoveryToken": logging.info(" Recovery Token Challenge for name: %s.", domain) return challenge_util.RecTokenChall(domain) elif chall["type"] == "recoveryContact": logging.info(" Recovery Contact Challenge for name: %s.", domain) return challenge_util.RecContactChall( domain, chall.get("activationURL", None), chall.get("successURL", None), chall.get("contact", None)) elif chall["type"] == "proofOfPossession": logging.info(" Proof-of-Possession Challenge for name: " "%s", domain) return challenge_util.PopChall( domain, chall["alg"], chall["nonce"], chall["hints"]) else: raise errors.LetsEncryptClientError( "Unimplemented Client Challenge: %s" % chall["type"])
def _challenge_factory(self, domain, path): """Construct Namedtuple Challenges :param str domain: domain of the enrollee :param list path: List of indices from `challenges`. :returns: dv_chall, list of DVChallenge type :class:`letsencrypt.client.achallenges.Indexed` cont_chall, list of ContinuityChallenge type :class:`letsencrypt.client.achallenges.Indexed` :rtype: tuple :raises errors.LetsEncryptClientError: If Challenge type is not recognized """ dv_chall = [] cont_chall = [] for index in path: chall = self.msgs[domain].challenges[index] if isinstance(chall, challenges.DVSNI): logging.info(" DVSNI challenge for %s.", domain) achall = achallenges.DVSNI( chall=chall, domain=domain, key=self.authkey[domain]) elif isinstance(chall, challenges.SimpleHTTPS): logging.info(" SimpleHTTPS challenge for %s.", domain) achall = achallenges.SimpleHTTPS( chall=chall, domain=domain, key=self.authkey[domain]) elif isinstance(chall, challenges.DNS): logging.info(" DNS challenge for %s.", domain) achall = achallenges.DNS(chall=chall, domain=domain) elif isinstance(chall, challenges.RecoveryToken): logging.info(" Recovery Token Challenge for %s.", domain) achall = achallenges.RecoveryToken(chall=chall, domain=domain) elif isinstance(chall, challenges.RecoveryContact): logging.info(" Recovery Contact Challenge for %s.", domain) achall = achallenges.RecoveryContact(chall=chall, domain=domain) elif isinstance(chall, challenges.ProofOfPossession): logging.info(" Proof-of-Possession Challenge for %s", domain) achall = achallenges.ProofOfPossession( chall=chall, domain=domain) else: raise errors.LetsEncryptClientError( "Received unsupported challenge of type: %s", chall.typ) ichall = achallenges.Indexed(achall=achall, index=index) if isinstance(chall, challenges.ContinuityChallenge): cont_chall.append(ichall) elif isinstance(chall, challenges.DVChallenge): dv_chall.append(ichall) return dv_chall, cont_chall
def validate_key_csr(privkey, csr=None): """Validate Key and CSR files. Verifies that the client key and csr arguments are valid and correspond to one another. This does not currently check the names in the CSR due to the inability to read SANs from CSRs in python crypto libraries. If csr is left as None, only the key will be validated. :param privkey: Key associated with CSR :type privkey: :class:`letsencrypt.client.le_util.Key` :param csr: CSR :type csr: :class:`letsencrypt.client.le_util.CSR` :raises letsencrypt.client.errors.LetsEncryptClientError: when validation fails """ # TODO: Handle all of these problems appropriately # The client can eventually do things like prompt the user # and allow the user to take more appropriate actions # Key must be readable and valid. if privkey.pem and not crypto_util.valid_privkey(privkey.pem): raise errors.LetsEncryptClientError( "The provided key is not a valid key") if csr: if csr.form == "der": csr_obj = M2Crypto.X509.load_request_der_string(csr.data) csr = le_util.CSR(csr.file, csr_obj.as_pem(), "der") # If CSR is provided, it must be readable and valid. if csr.data and not crypto_util.valid_csr(csr.data): raise errors.LetsEncryptClientError( "The provided CSR is not a valid CSR") # If both CSR and key are provided, the key must be the same key used # in the CSR. if csr.data and privkey.pem: if not crypto_util.csr_matches_pubkey( csr.data, privkey.pem): raise errors.LetsEncryptClientError( "The key and CSR do not match")
def send(self, msg): """Send ACME message to server. :param msg: ACME message (JSON serializable). :type msg: dict :raises: TypeError if `msg` is not JSON serializable or jsonschema.ValidationError if not valid ACME message or `errors.LetsEncryptClientError` in case of connection error or if response from server is not a valid ACME message. :returns: Server response message. :rtype: dict """ json_encoded = json.dumps(msg) acme.acme_object_validate(json_encoded) try: response = requests.post( self.server_url, data=json_encoded, headers={"Content-Type": "application/json"}, ) except requests.exceptions.RequestException as error: raise errors.LetsEncryptClientError( 'Sending ACME message to server has failed: %s' % error) try: acme.acme_object_validate(response.content) except ValueError: raise errors.LetsEncryptClientError( 'Server did not send JSON serializable message') except jsonschema.ValidationError as error: raise errors.LetsEncryptClientError( 'Response from server is not a valid ACME message') return response.json()
def challb_to_achall(challb, key, domain): """Converts a ChallengeBody object to an AnnotatedChallenge. :param challb: ChallengeBody :type challb: :class:`letsencrypt.acme.messages2.ChallengeBody` :param key: Key :type key: :class:`letsencrypt.client.le_util.Key` :param str domain: Domain of the challb :returns: Appropriate AnnotatedChallenge :rtype: :class:`letsencrypt.client.achallenges.AnnotatedChallenge` """ chall = challb.chall if isinstance(chall, challenges.DVSNI): logging.info(" DVSNI challenge for %s.", domain) return achallenges.DVSNI( challb=challb, domain=domain, key=key) elif isinstance(chall, challenges.SimpleHTTPS): logging.info(" SimpleHTTPS challenge for %s.", domain) return achallenges.SimpleHTTPS( challb=challb, domain=domain, key=key) elif isinstance(chall, challenges.DNS): logging.info(" DNS challenge for %s.", domain) return achallenges.DNS(challb=challb, domain=domain) elif isinstance(chall, challenges.RecoveryToken): logging.info(" Recovery Token Challenge for %s.", domain) return achallenges.RecoveryToken(challb=challb, domain=domain) elif isinstance(chall, challenges.RecoveryContact): logging.info(" Recovery Contact Challenge for %s.", domain) return achallenges.RecoveryContact( challb=challb, domain=domain) elif isinstance(chall, challenges.ProofOfPossession): logging.info(" Proof-of-Possession Challenge for %s", domain) return achallenges.ProofOfPossession( challb=challb, domain=domain) else: raise errors.LetsEncryptClientError( "Received unsupported challenge of type: %s", chall.typ)
def is_expected_msg(self, response, expected, delay=3, rounds=20): """Is reponse expected ACME message? :param response: ACME response message from server. :type response: dict :param expected: Name of the expected response ACME message type. :type expected: str :param delay: Number of seconds to delay before next round in case of ACME "defer" response message. :type delay: int :param rounds: Number of resend attempts in case of ACME "defer" reponse message. :type rounds: int :raises: Exception :returns: ACME response message from server. :rtype: dict """ for _ in xrange(rounds): if response["type"] == expected: return response elif response["type"] == "error": logger.error("%s: %s - More Info: %s" % (response["error"], response.get( "message", ""), response.get("moreInfo", ""))) raise errors.LetsEncryptClientError(response["error"]) elif response["type"] == "defer": logger.info("Waiting for %d seconds..." % delay) time.sleep(delay) response = self.send(acme.status_request(response["token"])) else: logger.fatal("Received unexpected message") logger.fatal("Expected: %s" % expected) logger.fatal("Received: " + response) sys.exit(33) logger.error("Server has deferred past the max of %d seconds" % (rounds * delay))
def send_and_receive_expected(self, msg, expected): """Send ACME message to server and return expected message. :param msg: ACME message (JSON serializable). :type acem_msg: dict :param expected: Name of the expected response ACME message type. :type expected: str :returns: ACME response message of expected type. :rtype: dict """ response = self.send(msg) try: return self.is_expected_msg(response, expected) except: # TODO: too generic exception raise errors.LetsEncryptClientError( 'Expected message (%s) not received' % expected)
def _challenge_factory(self, domain, path): """Construct Namedtuple Challenges :param str domain: domain of the enrollee :param list path: List of indices from `challenges`. :returns: dv_chall, list of :class:`letsencrypt.client.challenge_util.IndexedChall` client_chall, list of :class:`letsencrypt.client.challenge_util.IndexedChall` :rtype: tuple :raises errors.LetsEncryptClientError: If Challenge type is not recognized """ challenges = self.msgs[domain].challenges dv_chall = [] client_chall = [] for index in path: chall = challenges[index] # Authenticator Challenges if chall["type"] in constants.DV_CHALLENGES: dv_chall.append( challenge_util.IndexedChall( self._construct_dv_chall(chall, domain), index)) # Client Challenges elif chall["type"] in constants.CLIENT_CHALLENGES: client_chall.append( challenge_util.IndexedChall( self._construct_client_chall(chall, domain), index)) else: raise errors.LetsEncryptClientError( "Received unrecognized challenge of type: " "%s" % chall["type"]) return dv_chall, client_chall
def register(self): """New Registration with the ACME server.""" self.account = self.network.register_from_account(self.account) if self.account.terms_of_service: if not self.config.tos: # TODO: Replace with self.account.terms_of_service eula = pkg_resources.resource_string("letsencrypt", "EULA") agree = zope.component.getUtility(interfaces.IDisplay).yesno( eula, "Agree", "Cancel") else: agree = True if agree: self.account.regr = self.network.agree_to_tos(self.account.regr) else: # What is the proper response here... raise errors.LetsEncryptClientError("Must agree to TOS") self.account.save()
def ask(enhancement): """Display the enhancement to the user. :param str enhancement: One of the :class:`letsencrypt.client.CONFIG.ENHANCEMENTS` enhancements :returns: True if feature is desired, False otherwise :rtype: bool :raises letsencrypt.client.errors.LetsEncryptClientError: If the enhancement provided is not supported. """ try: # Call the appropriate function based on the enhancement return DISPATCH[enhancement]() except KeyError: logging.error("Unsupported enhancement given to ask(): %s", enhancement) raise errors.LetsEncryptClientError("Unsupported Enhancement")
def determine_authenticator(all_auths): """Returns a valid IAuthenticator. :param list all_auths: Where each is a :class:`letsencrypt.client.interfaces.IAuthenticator` object :returns: Valid Authenticator object or None :raises letsencrypt.client.errors.LetsEncryptClientError: If no authenticator is available. """ # Available Authenticator objects avail_auths = [] # Error messages for misconfigured authenticators errs = {} for pot_auth in all_auths: try: pot_auth.prepare() except errors.LetsEncryptMisconfigurationError as err: errs[pot_auth] = err except errors.LetsEncryptNoInstallationError: continue avail_auths.append(pot_auth) if len(avail_auths) > 1: auth = display_ops.choose_authenticator(avail_auths, errs) elif len(avail_auths) == 1: auth = avail_auths[0] else: raise errors.LetsEncryptClientError("No Authenticators available.") if auth is not None and auth in errs: logging.error( "Please fix the configuration for the Authenticator. " "The following error message was received: " "%s", errs[auth]) return return auth
def is_expected_msg(self, response, expected, delay=3, rounds=20): """Is response expected ACME message? :param response: ACME response message from server. :type response: :class:`letsencrypt.acme.messages.Message` :param expected: Expected response type. :type expected: subclass of :class:`letsencrypt.acme.messages.Message` :param int delay: Number of seconds to delay before next round in case of ACME "defer" response message. :param int rounds: Number of resend attempts in case of ACME "defer" response message. :returns: ACME response message from server. :rtype: :class:`letsencrypt.acme.messages.Message` :raises LetsEncryptClientError: if server sent ACME "error" message """ for _ in xrange(rounds): if isinstance(response, expected): return response elif isinstance(response, messages.Error): logging.error("%s", response) raise errors.LetsEncryptClientError(response.error) elif isinstance(response, messages.Defer): logging.info("Waiting for %d seconds...", delay) time.sleep(delay) response = self.send( messages.StatusRequest(token=response.token)) else: logging.fatal("Received unexpected message") logging.fatal("Expected: %s", expected) logging.fatal("Received: %s", response) sys.exit(33) logging.error("Server has deferred past the max of %d seconds", rounds * delay)
def from_email(cls, config, email): """Generate a new account from an email address. :param config: Configuration :type config: :class:`letsencrypt.client.interfaces.IConfig` :param str email: Email address :raises letsencrypt.client.errors.LetsEncryptClientError: If invalid email address is given. """ if not email or cls.safe_email(email): email = email if email else None le_util.make_or_verify_dir(config.account_keys_dir, 0o700, os.geteuid()) key = crypto_util.init_save_key(config.rsa_key_size, config.account_keys_dir, cls._get_config_filename(email)) return cls(config, key, email) raise errors.LetsEncryptClientError("Invalid email address.")
def log(self, level, data): raise errors.LetsEncryptClientError("No Logger defined")