Пример #1
0
    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: Certificate, private key, and certificate chain (all
            PEM-encoded).
        :rtype: `tuple` of `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)

        cert_pem = certr.body.as_pem()
        chain_pem = None
        if certr.cert_chain_uri is not None:
            chain_pem = self.network.fetch_chain(certr)

        if chain_pem is None:
            # XXX: just to stop RenewableCert from complaining; this is
            #      probably not a good solution
            chain_pem = ""
        else:
            chain_pem = chain_pem.as_pem()

        return cert_pem, cert_key.pem, chain_pem
    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
Пример #4
0
    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.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)
Пример #5
0
    def deploy_certificate(self, domains, privkey_path, cert_path, chain_path):
        """Install certificate

        :param list domains: list of domains to install the certificate
        :param str privkey_path: path to certificate private key
        :param str cert_path: certificate file path (optional)
        :param str chain_path: 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_path = None if chain_path is None else os.path.abspath(
            chain_path)

        for dom in domains:
            # TODO: Provide a fullchain reference for installers like
            #       nginx that want it
            self.installer.deploy_cert(dom, os.path.abspath(cert_path),
                                       os.path.abspath(privkey_path),
                                       chain_path)

        self.installer.save("Deployed Let's Encrypt Certificate")
        # sites may have been enabled / final cleanup
        self.installer.restart()

        display_ops.success_installation(domains)
Пример #6
0
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.le_util.Key`

    :param csr: CSR
    :type csr: :class:`letsencrypt.le_util.CSR`

    :raises letsencrypt.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 challb_to_achall(challb, key, domain):
    """Converts a ChallengeBody object to an AnnotatedChallenge.

    :param challb: ChallengeBody
    :type challb: :class:`acme.messages2.ChallengeBody`

    :param key: Key
    :type key: :class:`letsencrypt.le_util.Key`

    :param str domain: Domain of the challb

    :returns: Appropriate AnnotatedChallenge
    :rtype: :class:`letsencrypt.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.SimpleHTTP):
        logging.info("  SimpleHTTP challenge for %s.", domain)
        return achallenges.SimpleHTTP(
            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)
Пример #8
0
def ask(enhancement):
    """Display the enhancement to the user.

    :param str enhancement: One of the
        :class:`letsencrypt.CONFIG.ENHANCEMENTS` enhancements

    :returns: True if feature is desired, False otherwise
    :rtype: bool

    :raises letsencrypt.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")
Пример #9
0
    def register(self):
        """New Registration with the ACME server."""
        self.account = self.network.register_from_account(self.account)
        if self.account.terms_of_service is not None:
            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()
        self._report_new_account()
Пример #10
0
    def from_email(cls, config, email):
        """Generate a new account from an email address.

        :param config: Configuration
        :type config: :class:`letsencrypt.interfaces.IConfig`

        :param str email: Email address

        :raises letsencrypt.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.")