1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "qrcode-util.h"
8 #include "dlfcn-util.h"
9 #include "errno-util.h"
10 #include "homectl-recovery-key.h"
11 #include "libcrypt-util.h"
12 #include "locale-util.h"
13 #include "memory-util.h"
15 #include "random-util.h"
17 #include "terminal-util.h"
19 static int make_recovery_key(char **ret
) {
20 _cleanup_(erase_and_freep
) char *formatted
= NULL
;
21 _cleanup_(erase_and_freep
) uint8_t *key
= NULL
;
26 key
= new(uint8_t, MODHEX_RAW_LENGTH
);
30 r
= genuine_random_bytes(key
, MODHEX_RAW_LENGTH
, RANDOM_BLOCK
);
32 return log_error_errno(r
, "Failed to gather entropy for recovery key: %m");
34 /* Let's now format it as 64 modhex chars, and after each 8 chars insert a dash */
35 formatted
= new(char, MODHEX_FORMATTED_LENGTH
);
39 for (size_t i
= 0, j
= 0; i
< MODHEX_RAW_LENGTH
; i
++) {
40 formatted
[j
++] = modhex_alphabet
[key
[i
] >> 4];
41 formatted
[j
++] = modhex_alphabet
[key
[i
] & 0xF];
47 formatted
[MODHEX_FORMATTED_LENGTH
-1] = 0;
49 *ret
= TAKE_PTR(formatted
);
53 static int add_privileged(JsonVariant
**v
, const char *hashed
) {
54 _cleanup_(json_variant_unrefp
) JsonVariant
*e
= NULL
, *w
= NULL
, *l
= NULL
;
60 r
= json_build(&e
, JSON_BUILD_OBJECT(
61 JSON_BUILD_PAIR("type", JSON_BUILD_STRING("modhex64")),
62 JSON_BUILD_PAIR("hashedPassword", JSON_BUILD_STRING(hashed
))));
64 return log_error_errno(r
, "Failed to build recover key JSON object: %m");
66 json_variant_sensitive(e
);
68 w
= json_variant_ref(json_variant_by_key(*v
, "privileged"));
69 l
= json_variant_ref(json_variant_by_key(w
, "recoveryKey"));
71 r
= json_variant_append_array(&l
, e
);
73 return log_error_errno(r
, "Failed append recovery key: %m");
75 r
= json_variant_set_field(&w
, "recoveryKey", l
);
77 return log_error_errno(r
, "Failed to set recovery key array: %m");
79 r
= json_variant_set_field(v
, "privileged", w
);
81 return log_error_errno(r
, "Failed to update privileged field: %m");
86 static int add_public(JsonVariant
**v
) {
87 _cleanup_strv_free_
char **types
= NULL
;
92 r
= json_variant_strv(json_variant_by_key(*v
, "recoveryKeyType"), &types
);
94 return log_error_errno(r
, "Failed to parse recovery key type list: %m");
96 r
= strv_extend(&types
, "modhex64");
100 r
= json_variant_set_field_strv(v
, "recoveryKeyType", types
);
102 return log_error_errno(r
, "Failed to update recovery key types: %m");
107 static int add_secret(JsonVariant
**v
, const char *password
) {
108 _cleanup_(json_variant_unrefp
) JsonVariant
*w
= NULL
, *l
= NULL
;
109 _cleanup_(strv_free_erasep
) char **passwords
= NULL
;
115 w
= json_variant_ref(json_variant_by_key(*v
, "secret"));
116 l
= json_variant_ref(json_variant_by_key(w
, "password"));
118 r
= json_variant_strv(l
, &passwords
);
120 return log_error_errno(r
, "Failed to convert password array: %m");
122 r
= strv_extend(&passwords
, password
);
126 r
= json_variant_new_array_strv(&l
, passwords
);
128 return log_error_errno(r
, "Failed to allocate new password array JSON: %m");
130 json_variant_sensitive(l
);
132 r
= json_variant_set_field(&w
, "password", l
);
134 return log_error_errno(r
, "Failed to update password field: %m");
136 r
= json_variant_set_field(v
, "secret", w
);
138 return log_error_errno(r
, "Failed to update secret object: %m");
143 static int print_qr_code(const char *secret
) {
145 QRcode
* (*sym_QRcode_encodeString
)(const char *string
, int version
, QRecLevel level
, QRencodeMode hint
, int casesensitive
);
146 void (*sym_QRcode_free
)(QRcode
*qrcode
);
147 _cleanup_(dlclosep
) void *dl
= NULL
;
151 /* If this is not an UTF-8 system or ANSI colors aren't supported/disabled don't print any QR
153 if (!is_locale_utf8() || !colors_enabled())
156 dl
= dlopen("libqrencode.so.4", RTLD_LAZY
);
158 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
159 "QRCODE support is not installed: %s", dlerror());
161 r
= dlsym_many_and_warn(
164 &sym_QRcode_encodeString
, "QRcode_encodeString",
165 &sym_QRcode_free
, "QRcode_free",
170 qr
= sym_QRcode_encodeString(secret
, 0, QR_ECLEVEL_L
, QR_MODE_8
, 0);
174 fprintf(stderr
, "\nYou may optionally scan the recovery key off screen:\n\n");
176 write_qrcode(stderr
, qr
);
185 int identity_add_recovery_key(JsonVariant
**v
) {
186 _cleanup_(erase_and_freep
) char *password
= NULL
, *hashed
= NULL
;
191 /* First, let's generate a secret key */
192 r
= make_recovery_key(&password
);
196 /* Let's UNIX hash it */
197 r
= hash_password(password
, &hashed
);
199 return log_error_errno(errno_or_else(EINVAL
), "Failed to UNIX hash secret key: %m");
201 /* Let's now add the "privileged" version of the recovery key */
202 r
= add_privileged(v
, hashed
);
206 /* Let's then add the public information about the recovery key */
211 /* Finally, let's add the new key to the secret part, too */
212 r
= add_secret(v
, password
);
216 /* We output the key itself with a trailing newline to stdout and the decoration around it to stderr
221 "A secret recovery key has been generated for this account:\n\n"
223 emoji_enabled() ? special_glyph(SPECIAL_GLYPH_LOCK_AND_KEY
) : "",
224 emoji_enabled() ? " " : "",
228 fputs(password
, stdout
);
231 fputs(ansi_normal(), stderr
);
237 fputs("\nPlease save this secret recovery key at a secure location. It may be used to\n"
238 "regain access to the account if the other configured access credentials have\n"
239 "been lost or forgotten. The recovery key may be entered in place of a password\n"
240 "whenever authentication is requested.\n", stderr
);
243 print_qr_code(password
);