| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "ask-password-api.h" |
| 5 |
#include "cryptenroll-fido2.h" |
| 6 |
#include "cryptsetup-fido2.h" |
| 7 |
#include "cryptsetup-util.h" |
| 8 |
#include "fido2-util.h" |
| 9 |
#include "glyph-util.h" |
| 10 |
#include "hexdecoct.h" |
| 11 |
#include "iovec-util.h" |
| 12 |
#include "json-util.h" |
| 13 |
#include "libfido2-util.h" |
| 14 |
#include "pretty-print.h" |
| 15 |
#include "string-util.h" |
| 16 |
|
| 17 |
int load_volume_key_fido2( |
| 18 |
struct crypt_device *cd, |
| 19 |
const char *cd_node, |
| 20 |
const char *device, |
| 21 |
void *ret_vk, |
| 22 |
size_t *ret_vks) { |
| 23 |
|
| 24 |
#if HAVE_LIBFIDO2 |
| 25 |
_cleanup_(erase_and_freep) void *decrypted_key = NULL; |
| 26 |
_cleanup_(erase_and_freep) char *passphrase = NULL; |
| 27 |
size_t decrypted_key_size; |
| 28 |
ssize_t passphrase_size; |
| 29 |
int r; |
| 30 |
|
| 31 |
assert_se(cd); |
| 32 |
assert_se(cd_node); |
| 33 |
assert_se(ret_vk); |
| 34 |
assert_se(ret_vks); |
| 35 |
|
| 36 |
r = acquire_fido2_key_auto( |
| 37 |
cd, |
| 38 |
cd_node, |
| 39 |
cd_node, |
| 40 |
device, |
| 41 |
/* until= */ 0, |
| 42 |
"cryptenroll.fido2-pin", |
| 43 |
ASK_PASSWORD_PUSH_CACHE|ASK_PASSWORD_ACCEPT_CACHED, |
| 44 |
&decrypted_key, |
| 45 |
&decrypted_key_size); |
| 46 |
if (r == -EAGAIN) |
| 47 |
return log_error_errno(r, "FIDO2 token does not exist, or UV is blocked. Please try again."); |
| 48 |
if (r < 0) |
| 49 |
return r; |
| 50 |
|
| 51 |
/* Because cryptenroll requires a LUKS header, we can assume that this device is not |
| 52 |
* a PLAIN device. In this case, we need to base64 encode the secret to use as the passphrase */ |
| 53 |
passphrase_size = base64mem(decrypted_key, decrypted_key_size, &passphrase); |
| 54 |
if (passphrase_size < 0) |
| 55 |
return log_oom(); |
| 56 |
|
| 57 |
r = sym_crypt_volume_key_get( |
| 58 |
cd, |
| 59 |
CRYPT_ANY_SLOT, |
| 60 |
ret_vk, |
| 61 |
ret_vks, |
| 62 |
passphrase, |
| 63 |
passphrase_size); |
| 64 |
if (r < 0) |
| 65 |
return log_error_errno(r, "Unlocking via FIDO2 device failed: %m"); |
| 66 |
|
| 67 |
return r; |
| 68 |
#else |
| 69 |
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "FIDO2 unlocking not supported."); |
| 70 |
#endif |
| 71 |
} |
| 72 |
|
| 73 |
int enroll_fido2( |
| 74 |
struct crypt_device *cd, |
| 75 |
const struct iovec *volume_key, |
| 76 |
const char *device, |
| 77 |
Fido2EnrollFlags lock_with, |
| 78 |
int cred_alg, |
| 79 |
const char *salt_file, |
| 80 |
bool parameters_in_header) { |
| 81 |
|
| 82 |
#if HAVE_LIBFIDO2 |
| 83 |
_cleanup_(iovec_done_erase) struct iovec salt = {}; |
| 84 |
_cleanup_(erase_and_freep) void *secret = NULL; |
| 85 |
_cleanup_(erase_and_freep) char *base64_encoded = NULL; |
| 86 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 87 |
_cleanup_free_ char *keyslot_as_string = NULL; |
| 88 |
size_t cid_size, secret_size; |
| 89 |
_cleanup_free_ void *cid = NULL; |
| 90 |
ssize_t base64_encoded_size; |
| 91 |
const char *node, *un; |
| 92 |
int r, keyslot; |
| 93 |
|
| 94 |
assert_se(cd); |
| 95 |
assert_se(iovec_is_set(volume_key)); |
| 96 |
assert_se(device); |
| 97 |
|
| 98 |
assert_se(node = sym_crypt_get_device_name(cd)); |
| 99 |
|
| 100 |
un = strempty(sym_crypt_get_uuid(cd)); |
| 101 |
|
| 102 |
if (salt_file) |
| 103 |
r = fido2_read_salt_file( |
| 104 |
salt_file, |
| 105 |
/* offset= */ UINT64_MAX, |
| 106 |
/* client= */ "cryptenroll", |
| 107 |
/* node= */ un, |
| 108 |
&salt); |
| 109 |
else |
| 110 |
r = fido2_generate_salt(&salt); |
| 111 |
if (r < 0) |
| 112 |
return r; |
| 113 |
|
| 114 |
r = fido2_generate_hmac_hash( |
| 115 |
device, |
| 116 |
/* rp_id= */ "io.systemd.cryptsetup", |
| 117 |
/* rp_name= */ "Encrypted Volume", |
| 118 |
/* user_id= */ un, strlen(un), /* We pass the user ID and name as the same: the disk's UUID if we have it */ |
| 119 |
/* user_name= */ un, |
| 120 |
/* user_display_name= */ node, |
| 121 |
/* user_icon= */ NULL, |
| 122 |
/* askpw_icon= */ "drive-harddisk", |
| 123 |
/* askpw_credential= */ "cryptenroll.fido2-pin", |
| 124 |
lock_with, |
| 125 |
cred_alg, |
| 126 |
&salt, |
| 127 |
&cid, &cid_size, |
| 128 |
&secret, &secret_size, |
| 129 |
NULL, |
| 130 |
&lock_with); |
| 131 |
if (r < 0) |
| 132 |
return r; |
| 133 |
|
| 134 |
/* Before we use the secret, we base64 encode it, for compat with homed, and to make it easier to type in manually */ |
| 135 |
base64_encoded_size = base64mem(secret, secret_size, &base64_encoded); |
| 136 |
if (base64_encoded_size < 0) |
| 137 |
return log_error_errno(base64_encoded_size, "Failed to base64 encode secret key: %m"); |
| 138 |
|
| 139 |
r = cryptsetup_set_minimal_pbkdf(cd); |
| 140 |
if (r < 0) |
| 141 |
return log_error_errno(r, "Failed to set minimal PBKDF: %m"); |
| 142 |
|
| 143 |
keyslot = sym_crypt_keyslot_add_by_volume_key( |
| 144 |
cd, |
| 145 |
CRYPT_ANY_SLOT, |
| 146 |
volume_key->iov_base, |
| 147 |
volume_key->iov_len, |
| 148 |
base64_encoded, |
| 149 |
base64_encoded_size); |
| 150 |
if (keyslot < 0) |
| 151 |
return log_error_errno(keyslot, "Failed to add new FIDO2 key to %s: %m", node); |
| 152 |
|
| 153 |
if (parameters_in_header) { |
| 154 |
if (asprintf(&keyslot_as_string, "%i", keyslot) < 0) |
| 155 |
return log_oom(); |
| 156 |
|
| 157 |
r = sd_json_buildo(&v, |
| 158 |
SD_JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-fido2")), |
| 159 |
SD_JSON_BUILD_PAIR("keyslots", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_STRING(keyslot_as_string))), |
| 160 |
SD_JSON_BUILD_PAIR("fido2-credential", SD_JSON_BUILD_BASE64(cid, cid_size)), |
| 161 |
SD_JSON_BUILD_PAIR("fido2-salt", JSON_BUILD_IOVEC_BASE64(&salt)), |
| 162 |
SD_JSON_BUILD_PAIR("fido2-rp", JSON_BUILD_CONST_STRING("io.systemd.cryptsetup")), |
| 163 |
SD_JSON_BUILD_PAIR_BOOLEAN("fido2-clientPin-required", FLAGS_SET(lock_with, FIDO2ENROLL_PIN)), |
| 164 |
SD_JSON_BUILD_PAIR_BOOLEAN("fido2-up-required", FLAGS_SET(lock_with, FIDO2ENROLL_UP)), |
| 165 |
SD_JSON_BUILD_PAIR_BOOLEAN("fido2-uv-required", FLAGS_SET(lock_with, FIDO2ENROLL_UV))); |
| 166 |
if (r < 0) |
| 167 |
return log_error_errno(r, "Failed to prepare FIDO2 JSON token object: %m"); |
| 168 |
|
| 169 |
r = cryptsetup_add_token_json(cd, v); |
| 170 |
if (r < 0) |
| 171 |
return log_error_errno(r, "Failed to add FIDO2 JSON token to LUKS2 header: %m"); |
| 172 |
} else { |
| 173 |
_cleanup_free_ char *base64_encoded_cid = NULL, *link = NULL; |
| 174 |
|
| 175 |
r = base64mem(cid, cid_size, &base64_encoded_cid); |
| 176 |
if (r < 0) |
| 177 |
return log_error_errno(r, "Failed to base64 encode FIDO2 credential ID: %m"); |
| 178 |
|
| 179 |
r = terminal_urlify_man("crypttab", "5", &link); |
| 180 |
if (r < 0) |
| 181 |
return log_oom(); |
| 182 |
|
| 183 |
fflush(stdout); |
| 184 |
fprintf(stderr, |
| 185 |
"A FIDO2 credential has been registered for this volume:\n\n" |
| 186 |
" %s%sfido2-cid=%s", |
| 187 |
emoji_enabled() ? glyph(GLYPH_LOCK_AND_KEY) : "", |
| 188 |
emoji_enabled() ? " " : "", |
| 189 |
ansi_highlight()); |
| 190 |
fflush(stderr); |
| 191 |
|
| 192 |
fputs(base64_encoded_cid, stdout); |
| 193 |
fflush(stdout); |
| 194 |
|
| 195 |
fputs(ansi_normal(), stderr); |
| 196 |
fflush(stderr); |
| 197 |
|
| 198 |
fputc('\n', stdout); |
| 199 |
fflush(stdout); |
| 200 |
|
| 201 |
fprintf(stderr, |
| 202 |
"\nPlease save this FIDO2 credential ID. It is required when unlocking the volume\n" |
| 203 |
"using the associated FIDO2 keyslot which we just created. To configure automatic\n" |
| 204 |
"unlocking using this FIDO2 token, add an appropriate entry to your /etc/crypttab\n" |
| 205 |
"file, see %s for details.\n", link); |
| 206 |
fflush(stderr); |
| 207 |
} |
| 208 |
|
| 209 |
log_info("New FIDO2 token enrolled as key slot %i.", keyslot); |
| 210 |
return keyslot; |
| 211 |
#else |
| 212 |
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "FIDO2 key enrollment not supported."); |
| 213 |
#endif |
| 214 |
} |