Move tools_read_mk helper in libcryptsetup_cli.

This commit is contained in:
Ondrej Kozina
2020-07-30 09:57:46 +02:00
committed by Milan Broz
parent a985c12659
commit 42692418c2
9 changed files with 43 additions and 38 deletions

View File

@@ -25,4 +25,5 @@ libcryptsetup_cli_la_LIBADD = \
libcryptsetup_cli_la_SOURCES = \ libcryptsetup_cli_la_SOURCES = \
lib/utils_loop.c \ lib/utils_loop.c \
lib/utils_io.c \
lib/cli/cli.c lib/cli/cli.c

View File

@@ -34,6 +34,7 @@
#include "nls.h" #include "nls.h"
#include "utils_loop.h" #include "utils_loop.h"
#include "utils_io.h"
#include "libcryptsetup.h" #include "libcryptsetup.h"
#include "libcryptsetup_cli.h" #include "libcryptsetup_cli.h"
#include "cli_internal.h" #include "cli_internal.h"
@@ -303,6 +304,38 @@ int crypt_cli_get_key(const char *prompt,
return r; return r;
} }
int crypt_cli_read_mk(const char *file, char **key, size_t keysize)
{
int fd;
ssize_t ret;
if (!keysize || !key)
return -EINVAL;
*key = crypt_safe_alloc(keysize);
if (!*key)
return -ENOMEM;
fd = open(file, O_RDONLY);
if (fd == -1) {
log_err(_("Cannot read keyfile %s."), file);
goto fail;
}
ret = read_buffer(fd, *key, keysize);
if (ret < 0 || (size_t)ret != keysize) {
log_err(_("Cannot read %d bytes from keyfile %s."), keysize, file);
close(fd);
goto fail;
}
close(fd);
return 0;
fail:
crypt_safe_free(*key);
*key = NULL;
return -EINVAL;
}
static const struct tools_arg *find_arg_in_args(const char *name, const struct tools_arg *args, size_t args_len) static const struct tools_arg *find_arg_in_args(const char *name, const struct tools_arg *args, size_t args_len)
{ {
size_t i; size_t i;

View File

@@ -48,6 +48,8 @@ int crypt_cli_get_key(const char *prompt,
int timeout, int verify, int pwquality, int timeout, int verify, int pwquality,
struct crypt_device *cd, struct crypt_cli *ctx); struct crypt_device *cd, struct crypt_cli *ctx);
int crypt_cli_read_mk(const char *file, char **key, size_t keysize);
bool crypt_cli_arg_set(struct crypt_cli *ctx, const char *name); bool crypt_cli_arg_set(struct crypt_cli *ctx, const char *name);
int crypt_cli_arg_value(struct crypt_cli *ctx, const char *name, void *value); int crypt_cli_arg_value(struct crypt_cli *ctx, const char *name, void *value);

View File

@@ -1,6 +1,7 @@
CRYPTSETUP_CLI_1.0 { CRYPTSETUP_CLI_1.0 {
global: global:
crypt_cli_get_key; crypt_cli_get_key;
crypt_cli_read_mk;
crypt_cli_arg_set; crypt_cli_arg_set;
crypt_cli_arg_type; crypt_cli_arg_type;
crypt_cli_arg_value; crypt_cli_arg_value;

View File

@@ -1276,7 +1276,7 @@ static int _luksFormat(struct crypt_device **r_cd, char **r_password, size_t *r_
goto out; goto out;
if (ARG_SET(OPT_MASTER_KEY_FILE_ID)) { if (ARG_SET(OPT_MASTER_KEY_FILE_ID)) {
r = tools_read_mk(ARG_STR(OPT_MASTER_KEY_FILE_ID), &key, keysize); r = crypt_cli_read_mk(ARG_STR(OPT_MASTER_KEY_FILE_ID), &key, keysize);
if (r < 0) if (r < 0)
goto out; goto out;
} }
@@ -1385,7 +1385,7 @@ static int action_open_luks(void)
} else if (!keysize) } else if (!keysize)
keysize = ARG_UINT32(OPT_KEY_SIZE_ID) / 8; keysize = ARG_UINT32(OPT_KEY_SIZE_ID) / 8;
r = tools_read_mk(ARG_STR(OPT_MASTER_KEY_FILE_ID), &key, keysize); r = crypt_cli_read_mk(ARG_STR(OPT_MASTER_KEY_FILE_ID), &key, keysize);
if (r < 0) if (r < 0)
goto out; goto out;
r = crypt_activate_by_volume_key(cd, activated_name, r = crypt_activate_by_volume_key(cd, activated_name,
@@ -1634,7 +1634,7 @@ static int luksAddUnboundKey(void)
} }
if (ARG_SET(OPT_MASTER_KEY_FILE_ID)) { if (ARG_SET(OPT_MASTER_KEY_FILE_ID)) {
r = tools_read_mk(ARG_STR(OPT_MASTER_KEY_FILE_ID), &key, keysize); r = crypt_cli_read_mk(ARG_STR(OPT_MASTER_KEY_FILE_ID), &key, keysize);
if (r < 0) if (r < 0)
goto out; goto out;
@@ -1708,7 +1708,7 @@ static int action_luksAddKey(void)
} else if (!keysize) } else if (!keysize)
keysize = ARG_UINT32(OPT_KEY_SIZE_ID) / 8; keysize = ARG_UINT32(OPT_KEY_SIZE_ID) / 8;
r = tools_read_mk(ARG_STR(OPT_MASTER_KEY_FILE_ID), &key, keysize); r = crypt_cli_read_mk(ARG_STR(OPT_MASTER_KEY_FILE_ID), &key, keysize);
if (r < 0) if (r < 0)
goto out; goto out;

View File

@@ -109,7 +109,6 @@ int tools_is_cipher_null(const char *cipher);
int tools_wipe_progress(uint64_t size, uint64_t offset, void *usrptr); int tools_wipe_progress(uint64_t size, uint64_t offset, void *usrptr);
int tools_reencrypt_progress(uint64_t size, uint64_t offset, void *usrptr); int tools_reencrypt_progress(uint64_t size, uint64_t offset, void *usrptr);
int tools_read_mk(const char *file, char **key, int keysize);
int tools_write_mk(const char *file, const char *key, int keysize); int tools_write_mk(const char *file, const char *key, int keysize);
int tools_read_json_file(struct crypt_device *cd, const char *file, char **json, size_t *json_size); int tools_read_json_file(struct crypt_device *cd, const char *file, char **json, size_t *json_size);

View File

@@ -715,7 +715,7 @@ static int backup_luks_headers(struct reenc_ctx *rc)
rc->p[rc->keyslot].password, rc->p[rc->keyslot].passwordLen); rc->p[rc->keyslot].password, rc->p[rc->keyslot].passwordLen);
} else if (ARG_SET(OPT_MASTER_KEY_FILE_ID)) { } else if (ARG_SET(OPT_MASTER_KEY_FILE_ID)) {
log_dbg("Loading new key from file."); log_dbg("Loading new key from file.");
r = tools_read_mk(ARG_STR(OPT_MASTER_KEY_FILE_ID), &key, key_size); r = crypt_cli_read_mk(ARG_STR(OPT_MASTER_KEY_FILE_ID), &key, key_size);
} }
if (r < 0) if (r < 0)

View File

@@ -20,7 +20,6 @@
*/ */
#include "cryptsetup.h" #include "cryptsetup.h"
#include <termios.h>
void tools_passphrase_msg(int r) void tools_passphrase_msg(int r)
{ {
@@ -30,36 +29,6 @@ void tools_passphrase_msg(int r)
log_err(_("No usable keyslot is available.")); log_err(_("No usable keyslot is available."));
} }
int tools_read_mk(const char *file, char **key, int keysize)
{
int fd;
if (!keysize || !key)
return -EINVAL;
*key = crypt_safe_alloc(keysize);
if (!*key)
return -ENOMEM;
fd = open(file, O_RDONLY);
if (fd == -1) {
log_err(_("Cannot read keyfile %s."), file);
goto fail;
}
if (read_buffer(fd, *key, keysize) != keysize) {
log_err(_("Cannot read %d bytes from keyfile %s."), keysize, file);
close(fd);
goto fail;
}
close(fd);
return 0;
fail:
crypt_safe_free(*key);
*key = NULL;
return -EINVAL;
}
int tools_write_mk(const char *file, const char *key, int keysize) int tools_write_mk(const char *file, const char *key, int keysize)
{ {
int fd, r = -EINVAL; int fd, r = -EINVAL;

View File

@@ -176,7 +176,7 @@ static int _activate(const char *dm_device,
goto out; goto out;
} }
signature_size = st.st_size; signature_size = st.st_size;
r = tools_read_mk(ARG_STR(OPT_ROOT_HASH_SIGNATURE_ID), &signature, signature_size); r = crypt_cli_read_mk(ARG_STR(OPT_ROOT_HASH_SIGNATURE_ID), &signature, signature_size);
if (r < 0) { if (r < 0) {
log_err(_("Cannot read signature file %s."), ARG_STR(OPT_ROOT_HASH_SIGNATURE_ID)); log_err(_("Cannot read signature file %s."), ARG_STR(OPT_ROOT_HASH_SIGNATURE_ID));
goto out; goto out;