cryptenroll: support for enrolling FIDO2 tokens in manual mode

systemd-cryptsetup supports a FIDO2 mode with manual parameters, where
the user provides all the information necessary for recreating the
secret, such as: credential ID, relaying party ID and the salt. This
feature works great for implementing 2FA schemes, where the salt file
is for example a secret unsealed from the TPM or some other source.
While the unlocking part is quite straightforward to set up, enrolling
such a keyslot - not so easy. There is no clearly documented
way on how to set this up and online resources are scarce on this topic
too. By implementing a straightforward way to enroll such a keyslot
directly from systemd-cryptenroll we streamline the enrollment process
and reduce chances for user error when doing such things manually.
This commit is contained in:
Kamil Szczęk
2024-06-20 14:26:24 +02:00
committed by Lennart Poettering
parent ac6eb58f09
commit e262205eb7
14 changed files with 272 additions and 104 deletions
+15 -20
View File
@@ -5,8 +5,10 @@
#include "ask-password-api.h"
#include "cryptsetup-fido2.h"
#include "env-util.h"
#include "fido2-util.h"
#include "fileio.h"
#include "hexdecoct.h"
#include "iovec-util.h"
#include "libfido2-util.h"
#include "parse-util.h"
#include "random-util.h"
@@ -33,10 +35,9 @@ int acquire_fido2_key(
_cleanup_(erase_and_freep) char *envpw = NULL;
_cleanup_strv_free_erase_ char **pins = NULL;
_cleanup_free_ void *loaded_salt = NULL;
_cleanup_(iovec_done_erase) struct iovec loaded_salt = {};
bool device_exists = false;
const char *salt;
size_t salt_size;
struct iovec salt;
int r;
if ((required & (FIDO2ENROLL_PIN | FIDO2ENROLL_UP | FIDO2ENROLL_UV)) && FLAGS_SET(askpw_flags, ASK_PASSWORD_HEADLESS))
@@ -48,23 +49,17 @@ int acquire_fido2_key(
assert(cid);
assert(key_file || key_data);
if (key_data) {
salt = key_data;
salt_size = key_data_size;
} else {
_cleanup_free_ char *bindname = NULL;
if (key_data)
salt = IOVEC_MAKE(key_data, key_data_size);
else {
if (key_file_size > 0)
log_debug("Ignoring 'keyfile-size=' option for a FIDO2 salt file.");
/* If we read the salt via AF_UNIX, make this client recognizable */
if (asprintf(&bindname, "@%" PRIx64"/cryptsetup-fido2/%s", random_u64(), volume_name) < 0)
return log_oom();
r = read_full_file_full(
AT_FDCWD, key_file,
key_file_offset == 0 ? UINT64_MAX : key_file_offset,
key_file_size == 0 ? SIZE_MAX : key_file_size,
READ_FULL_FILE_CONNECT_SOCKET,
bindname,
(char**) &loaded_salt, &salt_size);
r = fido2_read_salt_file(
key_file, key_file_offset,
/* client= */ "cryptsetup",
/* node= */ volume_name,
&loaded_salt);
if (r < 0)
return r;
@@ -102,7 +97,7 @@ int acquire_fido2_key(
r = fido2_use_hmac_hash(
device,
rp_id ?: "io.systemd.cryptsetup",
salt, salt_size,
salt.iov_base, salt.iov_len,
cid, cid_size,
pins,
required,
+44
View File
@@ -0,0 +1,44 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "fido2-util.h"
#include "fileio.h"
#include "libfido2-util.h"
#include "random-util.h"
int fido2_generate_salt(struct iovec *ret_salt) {
_cleanup_(iovec_done) struct iovec salt = {};
int r;
r = crypto_random_bytes_allocate_iovec(FIDO2_SALT_SIZE, &salt);
if (r < 0)
return log_error_errno(r, "Failed to generate FIDO2 salt: %m");
*ret_salt = TAKE_STRUCT(salt);
return 0;
}
int fido2_read_salt_file(const char *filename, uint64_t offset, const char *client, const char *node, struct iovec *ret_salt) {
_cleanup_(iovec_done_erase) struct iovec salt = {};
_cleanup_free_ char *bind_name = NULL;
int r;
/* If we read the salt via AF_UNIX, make the client recognizable */
if (asprintf(&bind_name, "@%" PRIx64"/%s-fido2-salt/%s", random_u64(), client, node) < 0)
return log_oom();
r = read_full_file_full(
AT_FDCWD, filename,
offset == 0 ? UINT64_MAX : offset,
/* size= */ FIDO2_SALT_SIZE,
READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|
READ_FULL_FILE_CONNECT_SOCKET|READ_FULL_FILE_FAIL_WHEN_LARGER,
bind_name, (char**) &salt.iov_base, &salt.iov_len);
if (r == -E2BIG || (r >= 0 && salt.iov_len != FIDO2_SALT_SIZE))
return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL),
"FIDO2 salt file must contain exactly %u bytes.", FIDO2_SALT_SIZE);
if (r < 0)
return log_error_errno(r, "Reading FIDO2 salt file '%s' failed: %m", filename);
*ret_salt = TAKE_STRUCT(salt);
return 0;
}
+9
View File
@@ -0,0 +1,9 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <stdint.h>
#include "iovec-util.h"
int fido2_generate_salt(struct iovec *ret_salt);
int fido2_read_salt_file(const char *filename, uint64_t offset, const char *client, const char *node, struct iovec *ret_salt);
+6 -20
View File
@@ -10,7 +10,6 @@
#include "glyph-util.h"
#include "log.h"
#include "memory-util.h"
#include "random-util.h"
#include "strv.h"
#include "unistd.h"
@@ -683,8 +682,6 @@ finish:
return r;
}
#define FIDO2_SALT_SIZE 32
int fido2_generate_hmac_hash(
const char *device,
const char *rp_id,
@@ -697,13 +694,13 @@ int fido2_generate_hmac_hash(
const char *askpw_credential,
Fido2EnrollFlags lock_with,
int cred_alg,
const struct iovec *salt,
void **ret_cid, size_t *ret_cid_size,
void **ret_salt, size_t *ret_salt_size,
void **ret_secret, size_t *ret_secret_size,
char **ret_usedpin,
Fido2EnrollFlags *ret_locked_with) {
_cleanup_(erase_and_freep) void *salt = NULL, *secret_copy = NULL;
_cleanup_(erase_and_freep) void *secret_copy = NULL;
_cleanup_(fido_assert_free_wrapper) fido_assert_t *a = NULL;
_cleanup_(fido_cred_free_wrapper) fido_cred_t *c = NULL;
_cleanup_(fido_dev_free_wrapper) fido_dev_t *d = NULL;
@@ -717,12 +714,10 @@ int fido2_generate_hmac_hash(
assert(device);
assert(ret_cid);
assert(ret_cid_size);
assert(ret_salt);
assert(ret_salt_size);
assert(ret_secret);
assert(ret_secret_size);
/* Construction is like this: we generate a salt of 32 bytes. We then ask the FIDO2 device to
/* Construction is like this: we read or generate a salt of 32 bytes. We then ask the FIDO2 device to
* HMAC-SHA256 it for us with its internal key. The result is the key used by LUKS and account
* authentication. LUKS and UNIX password auth all do their own salting before hashing, so that FIDO2
* device never sees the volume key.
@@ -731,25 +726,18 @@ int fido2_generate_hmac_hash(
*
* with: S → LUKS/account authentication key (never stored)
* I → internal key on FIDO2 device (stored in the FIDO2 device)
* D → salt we generate here (stored in the privileged part of the JSON record)
* D → salt (stored in the privileged part of the JSON record or read from a file/socket)
*
*/
assert(device);
assert((lock_with & ~(FIDO2ENROLL_PIN|FIDO2ENROLL_UP|FIDO2ENROLL_UV)) == 0);
assert(iovec_is_set(salt));
r = dlopen_libfido2();
if (r < 0)
return log_error_errno(r, "FIDO2 token support is not installed.");
salt = malloc(FIDO2_SALT_SIZE);
if (!salt)
return log_oom();
r = crypto_random_bytes(salt, FIDO2_SALT_SIZE);
if (r < 0)
return log_error_errno(r, "Failed to generate salt: %m");
d = sym_fido_dev_new();
if (!d)
return log_oom();
@@ -935,7 +923,7 @@ int fido2_generate_hmac_hash(
return log_error_errno(SYNTHETIC_ERRNO(EIO),
"Failed to enable HMAC-SECRET extension on FIDO2 assertion: %s", sym_fido_strerr(r));
r = sym_fido_assert_set_hmac_salt(a, salt, FIDO2_SALT_SIZE);
r = sym_fido_assert_set_hmac_salt(a, salt->iov_base, salt->iov_len);
if (r != FIDO_OK)
return log_error_errno(SYNTHETIC_ERRNO(EIO),
"Failed to set salt on FIDO2 assertion: %s", sym_fido_strerr(r));
@@ -1072,8 +1060,6 @@ int fido2_generate_hmac_hash(
*ret_cid = TAKE_PTR(cid_copy);
*ret_cid_size = cid_size;
*ret_salt = TAKE_PTR(salt);
*ret_salt_size = FIDO2_SALT_SIZE;
*ret_secret = TAKE_PTR(secret_copy);
*ret_secret_size = secret_size;
+4 -1
View File
@@ -1,8 +1,11 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "iovec-util.h"
#include "macro.h"
#define FIDO2_SALT_SIZE 32U
typedef enum Fido2EnrollFlags {
FIDO2ENROLL_PIN = 1 << 0,
FIDO2ENROLL_UP = 1 << 1, /* User presence (ie: touching token) */
@@ -116,8 +119,8 @@ int fido2_generate_hmac_hash(
const char *askpw_credential,
Fido2EnrollFlags lock_with,
int cred_alg,
const struct iovec *salt,
void **ret_cid, size_t *ret_cid_size,
void **ret_salt, size_t *ret_salt_size,
void **ret_secret, size_t *ret_secret_size,
char **ret_usedpin,
Fido2EnrollFlags *ret_locked_with);
+1
View File
@@ -69,6 +69,7 @@ shared_sources = files(
'exit-status.c',
'extension-util.c',
'fdset.c',
'fido2-util.c',
'fileio-label.c',
'find-esp.c',
'firewall-util-nft.c',