| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "ansi-color.h" |
| 5 |
#include "cryptenroll-recovery.h" |
| 6 |
#include "cryptsetup-util.h" |
| 7 |
#include "glyph-util.h" |
| 8 |
#include "iovec-util.h" |
| 9 |
#include "json-util.h" |
| 10 |
#include "log.h" |
| 11 |
#include "qrcode-util.h" |
| 12 |
#include "recovery-key.h" |
| 13 |
|
| 14 |
int enroll_recovery( |
| 15 |
struct crypt_device *cd, |
| 16 |
const struct iovec *volume_key) { |
| 17 |
|
| 18 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 19 |
_cleanup_(erase_and_freep) char *password = NULL; |
| 20 |
_cleanup_free_ char *keyslot_as_string = NULL; |
| 21 |
int keyslot, r, q; |
| 22 |
const char *node; |
| 23 |
|
| 24 |
assert_se(cd); |
| 25 |
assert_se(iovec_is_set(volume_key)); |
| 26 |
|
| 27 |
assert_se(node = sym_crypt_get_device_name(cd)); |
| 28 |
|
| 29 |
r = make_recovery_key(&password); |
| 30 |
if (r < 0) |
| 31 |
return log_error_errno(r, "Failed to generate recovery key: %m"); |
| 32 |
|
| 33 |
r = cryptsetup_set_minimal_pbkdf(cd); |
| 34 |
if (r < 0) |
| 35 |
return log_error_errno(r, "Failed to set minimal PBKDF: %m"); |
| 36 |
|
| 37 |
keyslot = sym_crypt_keyslot_add_by_volume_key( |
| 38 |
cd, |
| 39 |
CRYPT_ANY_SLOT, |
| 40 |
volume_key->iov_base, |
| 41 |
volume_key->iov_len, |
| 42 |
password, |
| 43 |
strlen(password)); |
| 44 |
if (keyslot < 0) |
| 45 |
return log_error_errno(keyslot, "Failed to add new recovery key to %s: %m", node); |
| 46 |
|
| 47 |
fflush(stdout); |
| 48 |
fprintf(stderr, |
| 49 |
"A secret recovery key has been generated for this volume:\n\n" |
| 50 |
" %s%s%s", |
| 51 |
emoji_enabled() ? glyph(GLYPH_LOCK_AND_KEY) : "", |
| 52 |
emoji_enabled() ? " " : "", |
| 53 |
ansi_highlight()); |
| 54 |
fflush(stderr); |
| 55 |
|
| 56 |
fputs(password, stdout); |
| 57 |
fflush(stdout); |
| 58 |
|
| 59 |
fputs(ansi_normal(), stderr); |
| 60 |
fflush(stderr); |
| 61 |
|
| 62 |
fputc('\n', stdout); |
| 63 |
fflush(stdout); |
| 64 |
|
| 65 |
fputs("\nPlease save this secret recovery key at a secure location. It may be used to\n" |
| 66 |
"regain access to the volume if the other configured access credentials have\n" |
| 67 |
"been lost or forgotten. The recovery key may be entered in place of a password\n" |
| 68 |
"whenever authentication is requested.\n", stderr); |
| 69 |
fflush(stderr); |
| 70 |
|
| 71 |
(void) print_qrcode(stderr, "Optionally scan the recovery key for safekeeping", password); |
| 72 |
|
| 73 |
if (asprintf(&keyslot_as_string, "%i", keyslot) < 0) { |
| 74 |
r = log_oom(); |
| 75 |
goto rollback; |
| 76 |
} |
| 77 |
|
| 78 |
r = sd_json_buildo(&v, |
| 79 |
SD_JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-recovery")), |
| 80 |
SD_JSON_BUILD_PAIR("keyslots", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_STRING(keyslot_as_string)))); |
| 81 |
if (r < 0) { |
| 82 |
log_error_errno(r, "Failed to prepare recovery key JSON token object: %m"); |
| 83 |
goto rollback; |
| 84 |
} |
| 85 |
|
| 86 |
r = cryptsetup_add_token_json(cd, v); |
| 87 |
if (r < 0) { |
| 88 |
log_error_errno(r, "Failed to add recovery JSON token to LUKS2 header: %m"); |
| 89 |
goto rollback; |
| 90 |
} |
| 91 |
|
| 92 |
log_info("New recovery key enrolled as key slot %i.", keyslot); |
| 93 |
return keyslot; |
| 94 |
|
| 95 |
rollback: |
| 96 |
q = sym_crypt_keyslot_destroy(cd, keyslot); |
| 97 |
if (q < 0) |
| 98 |
log_debug_errno(q, "Unable to remove key slot we just added again, can't rollback, sorry: %m"); |
| 99 |
|
| 100 |
return r; |
| 101 |
} |