def _validate_csr_key_cli(self):
        """Validate CSR and key files.

        Verifies that the client key and csr arguments are valid and
        correspond to one another.

        :raises LetsEncryptClientError: if 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

        # If CSR is provided, it must be readable and valid.
        if self.csr.data and not crypto_util.valid_csr(self.csr.data):
            raise errors.LetsEncryptClientError(
                "The provided CSR is not a valid CSR")

        # If key is provided, it must be readable and valid.
        if (self.privkey.pem and
                not crypto_util.valid_privkey(self.privkey.pem)):
            raise errors.LetsEncryptClientError(
                "The provided key is not a valid key")

        # If CSR and key are provided, the key must be the same key used
        # in the CSR.
        if self.csr.data and self.privkey.pem:
            if not crypto_util.csr_matches_pubkey(
                    self.csr.data, self.privkey.pem):
                raise errors.LetsEncryptClientError(
                    "The key and CSR do not match")
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 _validate_csr_key_cli(self):
        """Validate CSR and key files.

        Verifies that the client key and csr arguments are valid and
        correspond to one another.

        """
        # 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

        # If CSR is provided, the private key should also be provided.
        if self.csr_file and not self.key_file:
            logger.fatal(("Please provide the private key file used in "
                          "generating the provided CSR"))
            sys.exit(1)
        # If CSR is provided, it must be readable and valid.
        try:
            if self.csr_file and not crypto_util.valid_csr(self.csr_file):
                raise Exception("The provided CSR is not a valid CSR")
        except IOError:
            raise Exception("The provided CSR could not be read")
        # If key is provided, it must be readable and valid.
        try:
            if self.key_file and not crypto_util.valid_privkey(self.key_file):
                raise Exception("The provided key is not a valid key")
        except IOError:
            raise Exception("The provided key could not be read")

        # If CSR and key are provided, the key must be the same key used
        # in the CSR.
        if self.csr_file and self.key_file:
            try:
                if not crypto_util.csr_matches_pubkey(self.csr_file,
                                                      self.key_file):
                    raise Exception("The key and CSR do not match")
            except IOError:
                raise Exception("The key or CSR files could not be read")
    def _validate_csr_key_cli(self):
        """Validate CSR and key files.

        Verifies that the client key and csr arguments are valid and
        correspond to one another.

        """
        # 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

        # If CSR is provided, the private key should also be provided.
        if self.csr_file and not self.key_file:
            logger.fatal(("Please provide the private key file used in "
                          "generating the provided CSR"))
            sys.exit(1)
        # If CSR is provided, it must be readable and valid.
        try:
            if self.csr_file and not crypto_util.valid_csr(self.csr_file):
                raise Exception("The provided CSR is not a valid CSR")
        except IOError:
            raise Exception("The provided CSR could not be read")
        # If key is provided, it must be readable and valid.
        try:
            if self.key_file and not crypto_util.valid_privkey(self.key_file):
                raise Exception("The provided key is not a valid key")
        except IOError:
            raise Exception("The provided key could not be read")

        # If CSR and key are provided, the key must be the same key used
        # in the CSR.
        if self.csr_file and self.key_file:
            try:
                if not crypto_util.csr_matches_pubkey(
                        self.csr_file, self.key_file):
                    raise Exception("The key and CSR do not match")
            except IOError:
                raise Exception("The key or CSR files could not be read")
 def _call_testdata(cls, name, privkey):
     from letsencrypt.client.crypto_util import csr_matches_pubkey
     return csr_matches_pubkey(pkg_resources.resource_string(
         __name__, os.path.join('testdata', name)), privkey)
Exemple #6
0
 def _call_testdata(cls, name, privkey):
     from letsencrypt.client.crypto_util import csr_matches_pubkey
     return csr_matches_pubkey(
         pkg_resources.resource_string(__name__,
                                       os.path.join('testdata', name)),
         privkey)