Ejemplo n.º 1
0
def init_save_csr(privkey, names, path):
    """Initialize a CSR with the given private key.

    :param privkey: Key to include in the CSR
    :type privkey: :class:`certbot.util.Key`

    :param set names: `str` names to include in the CSR

    :param str path: Certificate save directory.

    :returns: CSR
    :rtype: :class:`certbot.util.CSR`

    """
    config = zope.component.getUtility(interfaces.IConfig)

    csr_pem = acme_crypto_util.make_csr(
        privkey.pem, names, must_staple=config.must_staple)

    # Save CSR
    util.make_or_verify_dir(path, 0o755, os.geteuid(),
                               config.strict_permissions)
    csr_f, csr_filename = util.unique_file(
        os.path.join(path, "csr-certbot.pem"), 0o644, "wb")
    with csr_f:
        csr_f.write(csr_pem)
    logger.debug("Creating CSR: %s", csr_filename)

    return util.CSR(csr_filename, csr_pem, "pem")
Ejemplo n.º 2
0
def init_save_key(key_size, key_dir, keyname="key-certbot.pem"):
    """Initializes and saves a privkey.

    Inits key and saves it in PEM format on the filesystem.

    .. note:: keyname is the attempted filename, it may be different if a file
        already exists at the path.

    :param int key_size: RSA key size in bits
    :param str key_dir: Key save directory.
    :param str keyname: Filename of key

    :returns: Key
    :rtype: :class:`certbot.util.Key`

    :raises ValueError: If unable to generate the key given key_size.

    """
    try:
        key_pem = make_key(key_size)
    except ValueError as err:
        logger.exception(err)
        raise err

    config = zope.component.getUtility(interfaces.IConfig)
    # Save file
    util.make_or_verify_dir(key_dir, 0o700, os.geteuid(),
                            config.strict_permissions)
    key_f, key_path = util.unique_file(
        os.path.join(key_dir, keyname), 0o600, "wb")
    with key_f:
        key_f.write(key_pem)
    logger.debug("Generating key (%d bits): %s", key_size, key_path)

    return util.Key(key_path, key_pem)
Ejemplo n.º 3
0
 def _get_snakeoil_paths(self):
     # TODO: generate only once
     tmp_dir = os.path.join(self.config.work_dir, "snakeoil")
     le_key = crypto_util.init_save_key(key_size=1024, key_dir=tmp_dir, keyname="key.pem")
     key = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, le_key.pem)
     cert = acme_crypto_util.gen_ss_cert(key, domains=[socket.gethostname()])
     cert_pem = OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
     cert_file, cert_path = util.unique_file(os.path.join(tmp_dir, "cert.pem"), mode="wb")
     with cert_file:
         cert_file.write(cert_pem)
     return cert_path, le_key.file
Ejemplo n.º 4
0
def _open_pem_file(cli_arg_path, pem_path):
    """Open a pem file.

    If cli_arg_path was set by the client, open that.
    Otherwise, uniquify the file path.

    :param str cli_arg_path: the cli arg name, e.g. cert_path
    :param str pem_path: the pem file path to open

    :returns: a tuple of file object and its absolute file path

    """
    if cli.set_by_cli(cli_arg_path):
        return util.safe_open(pem_path, chmod=0o644, mode="wb"),\
            os.path.abspath(pem_path)
    uniq = util.unique_file(pem_path, 0o644, "wb")
    return uniq[0], os.path.abspath(uniq[1])
Ejemplo n.º 5
0
def _open_pem_file(cli_arg_path, pem_path):
    """Open a pem file.

    If cli_arg_path was set by the client, open that.
    Otherwise, uniquify the file path.

    :param str cli_arg_path: the cli arg name, e.g. cert_path
    :param str pem_path: the pem file path to open

    :returns: a tuple of file object and its absolute file path

    """
    if cli.set_by_cli(cli_arg_path):
        return util.safe_open(pem_path, chmod=0o644, mode="wb"),\
            os.path.abspath(pem_path)
    else:
        uniq = util.unique_file(pem_path, 0o644, "wb")
        return uniq[0], os.path.abspath(uniq[1])
Ejemplo n.º 6
0
 def _get_snakeoil_paths(self):
     # TODO: generate only once
     tmp_dir = os.path.join(self.config.work_dir, "snakeoil")
     le_key = crypto_util.init_save_key(key_size=1024,
                                        key_dir=tmp_dir,
                                        keyname="key.pem")
     key = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM,
                                          le_key.pem)
     cert = acme_crypto_util.gen_ss_cert(key,
                                         domains=[socket.gethostname()])
     cert_pem = OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM,
                                                cert)
     cert_file, cert_path = util.unique_file(os.path.join(
         tmp_dir, "cert.pem"),
                                             mode="wb")
     with cert_file:
         cert_file.write(cert_pem)
     return cert_path, le_key.file
Ejemplo n.º 7
0
def init_save_key(key_size,
                  key_dir,
                  ec_key_size=384,
                  key_type='rsa',
                  keyname="key-certbot.pem"):
    """Initializes and saves a privkey.

    Inits key and saves it in PEM format on the filesystem.

    .. note:: keyname is the attempted filename, it may be different if a file
        already exists at the path.

    :param int rsa_key_size: RSA key size in bits
    :param int ec_key_size: EC key size in bits
    :param str key_dir: Key save directory.
    :param str keyname: Filename of key

    :returns: Key
    :rtype: :class:`certbot.util.Key`

    :raises ValueError: If unable to generate the key given key_size.

    """
    try:
        key_pem = make_key(rsa_bits=key_size,
                           ec_bits=ec_key_size,
                           key_type=key_type)
    except ValueError as err:
        logger.error("", exc_info=True)
        raise err

    config = zope.component.getUtility(interfaces.IConfig)
    # Save file
    util.make_or_verify_dir(key_dir, 0o700, config.strict_permissions)
    key_f, key_path = util.unique_file(os.path.join(key_dir, keyname), 0o600,
                                       "wb")
    with key_f:
        key_f.write(key_pem)
    if key_type.lower() == 'rsa':
        logger.debug("Generating RSA key (%d bits): %s", key_size, key_path)
    else:
        logger.debug("Generating EC key (%d bits): %s", ec_key_size, key_path)

    return util.Key(key_path, key_pem)
Ejemplo n.º 8
0
def init_save_key(key_size, key_dir, keyname="key-certbot.pem"):
    """Initializes and saves a privkey.

    Inits key and saves it in PEM format on the filesystem.

    .. note:: keyname is the attempted filename, it may be different if a file
        already exists at the path.

    :param int key_size: RSA key size in bits
    :param str key_dir: Key save directory.
    :param str keyname: Filename of key

    :returns: Key
    :rtype: :class:`certbot.util.Key`

    :raises ValueError: If unable to generate the key given key_size.

    """
    try:
        key_pem = make_key(key_size)
    except ValueError as err:
        logger.exception(err)
        raise err

    config = zope.component.getUtility(interfaces.IConfig)
    # Save file
    util.make_or_verify_dir(key_dir, 0o700, os.geteuid(),
                            config.strict_permissions)
    if config.dry_run:
        key_path = None
        logger.debug("Generating key (%d bits), not saving to file", key_size)
    else:
        key_f, key_path = util.unique_file(os.path.join(key_dir, keyname),
                                           0o600, "wb")
        with key_f:
            key_f.write(key_pem)
        logger.debug("Generating key (%d bits): %s", key_size, key_path)

    return util.Key(key_path, key_pem)
Ejemplo n.º 9
0
 def _call(self, mode=0o600):
     from certbot.util import unique_file
     return unique_file(self.default_name, mode)
Ejemplo n.º 10
0
 def _call(self, mode=0o600):
     from certbot.util import unique_file
     return unique_file(self.default_name, mode)