1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "errno-util.h"
4 #include "homectl-recovery-key.h"
5 #include "libcrypt-util.h"
6 #include "locale-util.h"
7 #include "memory-util.h"
9 #include "qrcode-util.h"
10 #include "random-util.h"
12 #include "terminal-util.h"
14 static int make_recovery_key(char **ret
) {
15 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
16 _cleanup_(erase_and_freep
) uint8_t *key
= NULL
;
21 key
= new(uint8_t, MODHEX_RAW_LENGTH
);
25 r
= genuine_random_bytes(key
, MODHEX_RAW_LENGTH
, RANDOM_BLOCK
);
27 return log_error_errno(r
, "Failed to gather entropy for recovery key: %m");
29 /* Let's now format it as 64 modhex chars, and after each 8 chars insert a dash */
30 formatted
= new(char, MODHEX_FORMATTED_LENGTH
);
34 for (size_t i
= 0, j
= 0; i
< MODHEX_RAW_LENGTH
; i
++) {
35 formatted
[j
++] = modhex_alphabet
[key
[i
] >> 4];
36 formatted
[j
++] = modhex_alphabet
[key
[i
] & 0xF];
42 formatted
[MODHEX_FORMATTED_LENGTH
-1] = 0;
44 *ret
= TAKE_PTR(formatted
);
48 static int add_privileged(JsonVariant
**v
, const char *hashed
) {
49 _cleanup_(json_variant_unrefp
) JsonVariant
*e
= NULL
, *w
= NULL
, *l
= NULL
;
55 r
= json_build(&e
, JSON_BUILD_OBJECT(
56 JSON_BUILD_PAIR("type", JSON_BUILD_STRING("modhex64")),
57 JSON_BUILD_PAIR("hashedPassword", JSON_BUILD_STRING(hashed
))));
59 return log_error_errno(r
, "Failed to build recover key JSON object: %m");
61 json_variant_sensitive(e
);
63 w
= json_variant_ref(json_variant_by_key(*v
, "privileged"));
64 l
= json_variant_ref(json_variant_by_key(w
, "recoveryKey"));
66 r
= json_variant_append_array(&l
, e
);
68 return log_error_errno(r
, "Failed append recovery key: %m");
70 r
= json_variant_set_field(&w
, "recoveryKey", l
);
72 return log_error_errno(r
, "Failed to set recovery key array: %m");
74 r
= json_variant_set_field(v
, "privileged", w
);
76 return log_error_errno(r
, "Failed to update privileged field: %m");
81 static int add_public(JsonVariant
**v
) {
82 _cleanup_strv_free_
char **types
= NULL
;
87 r
= json_variant_strv(json_variant_by_key(*v
, "recoveryKeyType"), &types
);
89 return log_error_errno(r
, "Failed to parse recovery key type list: %m");
91 r
= strv_extend(&types
, "modhex64");
95 r
= json_variant_set_field_strv(v
, "recoveryKeyType", types
);
97 return log_error_errno(r
, "Failed to update recovery key types: %m");
102 static int add_secret(JsonVariant
**v
, const char *password
) {
103 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
, *l
= NULL
;
104 _cleanup_(strv_free_erasep
) char **passwords
= NULL
;
110 w
= json_variant_ref(json_variant_by_key(*v
, "secret"));
111 l
= json_variant_ref(json_variant_by_key(w
, "password"));
113 r
= json_variant_strv(l
, &passwords
);
115 return log_error_errno(r
, "Failed to convert password array: %m");
117 r
= strv_extend(&passwords
, password
);
121 r
= json_variant_new_array_strv(&l
, passwords
);
123 return log_error_errno(r
, "Failed to allocate new password array JSON: %m");
125 json_variant_sensitive(l
);
127 r
= json_variant_set_field(&w
, "password", l
);
129 return log_error_errno(r
, "Failed to update password field: %m");
131 r
= json_variant_set_field(v
, "secret", w
);
133 return log_error_errno(r
, "Failed to update secret object: %m");
138 int identity_add_recovery_key(JsonVariant
**v
) {
139 _cleanup_(erase_and_freep
) char *password
= NULL
, *hashed
= NULL
;
144 /* First, let's generate a secret key */
145 r
= make_recovery_key(&password
);
149 /* Let's UNIX hash it */
150 r
= hash_password(password
, &hashed
);
152 return log_error_errno(errno_or_else(EINVAL
), "Failed to UNIX hash secret key: %m");
154 /* Let's now add the "privileged" version of the recovery key */
155 r
= add_privileged(v
, hashed
);
159 /* Let's then add the public information about the recovery key */
164 /* Finally, let's add the new key to the secret part, too */
165 r
= add_secret(v
, password
);
169 /* We output the key itself with a trailing newline to stdout and the decoration around it to stderr
174 "A secret recovery key has been generated for this account:\n\n"
176 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_LOCK_AND_KEY
) : "",
177 emoji_enabled() ? " " : "",
181 fputs(password
, stdout
);
184 fputs(ansi_normal(), stderr
);
190 fputs("\nPlease save this secret recovery key at a secure location. It may be used to\n"
191 "regain access to the account if the other configured access credentials have\n"
192 "been lost or forgotten. The recovery key may be entered in place of a password\n"
193 "whenever authentication is requested.\n", stderr
);
196 (void) print_qrcode(stderr
, "You may optionally scan the recovery key off screen", password
);