| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-json.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "ansi-color.h" |
| 9 |
#include "chattr-util.h" |
| 10 |
#include "errno-util.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "fs-util.h" |
| 13 |
#include "fsprg.h" |
| 14 |
#include "hostname-setup.h" |
| 15 |
#include "hostname-util.h" |
| 16 |
#include "io-util.h" |
| 17 |
#include "journal-def.h" |
| 18 |
#include "journalctl.h" |
| 19 |
#include "journalctl-authenticate.h" |
| 20 |
#include "log.h" |
| 21 |
#include "memstream-util.h" |
| 22 |
#include "path-util.h" |
| 23 |
#include "qrcode-util.h" |
| 24 |
#include "random-util.h" |
| 25 |
#include "stat-util.h" |
| 26 |
#include "string-util.h" |
| 27 |
#include "terminal-util.h" |
| 28 |
#include "time-util.h" |
| 29 |
#include "tmpfile-util.h" |
| 30 |
|
| 31 |
#if HAVE_GCRYPT |
| 32 |
static int format_key( |
| 33 |
const void *seed, |
| 34 |
size_t seed_size, |
| 35 |
uint64_t start, |
| 36 |
uint64_t interval, |
| 37 |
char **ret) { |
| 38 |
|
| 39 |
_cleanup_(memstream_done) MemStream m = {}; |
| 40 |
FILE *f; |
| 41 |
|
| 42 |
assert(seed); |
| 43 |
assert(seed_size > 0); |
| 44 |
assert(ret); |
| 45 |
|
| 46 |
f = memstream_init(&m); |
| 47 |
if (!f) |
| 48 |
return -ENOMEM; |
| 49 |
|
| 50 |
for (size_t i = 0; i < seed_size; i++) { |
| 51 |
if (i > 0 && i % 3 == 0) |
| 52 |
fputc('-', f); |
| 53 |
fprintf(f, "%02x", ((uint8_t*) seed)[i]); |
| 54 |
} |
| 55 |
|
| 56 |
fprintf(f, "/%"PRIx64"-%"PRIx64, start, interval); |
| 57 |
|
| 58 |
return memstream_finalize(&m, ret, NULL); |
| 59 |
} |
| 60 |
#endif |
| 61 |
|
| 62 |
int action_setup_keys(void) { |
| 63 |
#if HAVE_GCRYPT |
| 64 |
_cleanup_(unlink_and_freep) char *tmpfile = NULL; |
| 65 |
_cleanup_close_ int fd = -EBADF; |
| 66 |
_cleanup_free_ char *path = NULL; |
| 67 |
size_t mpk_size, seed_size, state_size; |
| 68 |
uint8_t *mpk, *seed, *state; |
| 69 |
sd_id128_t machine, boot; |
| 70 |
uint64_t n; |
| 71 |
int r; |
| 72 |
|
| 73 |
assert(arg_action == ACTION_SETUP_KEYS); |
| 74 |
|
| 75 |
r = is_dir("/var/log/journal/", /* follow= */ false); |
| 76 |
if (r == 0) |
| 77 |
return log_error_errno(SYNTHETIC_ERRNO(ENOTDIR), |
| 78 |
"/var/log/journal is not a directory, must be using persistent logging for FSS."); |
| 79 |
if (r == -ENOENT) |
| 80 |
return log_error_errno(r, "Directory /var/log/journal/ does not exist, must be using persistent logging for FSS."); |
| 81 |
if (r < 0) |
| 82 |
return log_error_errno(r, "Failed to check if /var/log/journal/ is a directory: %m"); |
| 83 |
|
| 84 |
r = sd_id128_get_machine(&machine); |
| 85 |
if (r < 0) |
| 86 |
return log_error_errno(r, "Failed to get machine ID: %m"); |
| 87 |
|
| 88 |
r = sd_id128_get_boot(&boot); |
| 89 |
if (r < 0) |
| 90 |
return log_error_errno(r, "Failed to get boot ID: %m"); |
| 91 |
|
| 92 |
path = path_join("/var/log/journal/", SD_ID128_TO_STRING(machine), "/fss"); |
| 93 |
if (!path) |
| 94 |
return log_oom(); |
| 95 |
|
| 96 |
if (arg_force) { |
| 97 |
if (unlink(path) < 0 && errno != ENOENT) |
| 98 |
return log_error_errno(errno, "Failed to remove \"%s\": %m", path); |
| 99 |
} else if (access(path, F_OK) >= 0) |
| 100 |
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), |
| 101 |
"Sealing key file %s exists already. Use --force to recreate.", path); |
| 102 |
|
| 103 |
mpk_size = FSPRG_mskinbytes(FSPRG_RECOMMENDED_SECPAR); |
| 104 |
mpk = alloca_safe(mpk_size); |
| 105 |
|
| 106 |
seed_size = FSPRG_RECOMMENDED_SEEDLEN; |
| 107 |
seed = alloca_safe(seed_size); |
| 108 |
|
| 109 |
state_size = FSPRG_stateinbytes(FSPRG_RECOMMENDED_SECPAR); |
| 110 |
state = alloca_safe(state_size); |
| 111 |
|
| 112 |
if (!arg_quiet) |
| 113 |
log_info("Generating seed..."); |
| 114 |
r = crypto_random_bytes(seed, seed_size); |
| 115 |
if (r < 0) |
| 116 |
return log_error_errno(r, "Failed to acquire random seed: %m"); |
| 117 |
|
| 118 |
if (!arg_quiet) |
| 119 |
log_info("Generating key pair..."); |
| 120 |
r = FSPRG_GenMK(NULL, mpk, seed, seed_size, FSPRG_RECOMMENDED_SECPAR); |
| 121 |
if (r < 0) |
| 122 |
return log_error_errno(r, "Failed to generate key pair: %m"); |
| 123 |
|
| 124 |
if (!arg_quiet) |
| 125 |
log_info("Generating sealing key..."); |
| 126 |
r = FSPRG_GenState0(state, mpk, seed, seed_size); |
| 127 |
if (r < 0) |
| 128 |
return log_error_errno(r, "Failed to generate sealing key: %m"); |
| 129 |
|
| 130 |
assert(arg_interval > 0); |
| 131 |
n = now(CLOCK_REALTIME); |
| 132 |
n /= arg_interval; |
| 133 |
|
| 134 |
fd = open_tmpfile_linkable(path, O_WRONLY|O_CLOEXEC, &tmpfile); |
| 135 |
if (fd < 0) |
| 136 |
return log_error_errno(fd, "Failed to open a temporary file for %s: %m", path); |
| 137 |
|
| 138 |
r = chattr_secret(fd, CHATTR_WARN_UNSUPPORTED_FLAGS); |
| 139 |
if (r < 0) |
| 140 |
log_full_errno(ERRNO_IS_IOCTL_NOT_SUPPORTED(r) || arg_quiet ? LOG_DEBUG : LOG_WARNING, |
| 141 |
r, "Failed to set file attributes on a temporary file for '%s', ignoring: %m", path); |
| 142 |
|
| 143 |
struct FSSHeader h = { |
| 144 |
.signature = { 'K', 'S', 'H', 'H', 'R', 'H', 'L', 'P' }, |
| 145 |
.machine_id = machine, |
| 146 |
.boot_id = boot, |
| 147 |
.header_size = htole64(sizeof(h)), |
| 148 |
.start_usec = htole64(n * arg_interval), |
| 149 |
.interval_usec = htole64(arg_interval), |
| 150 |
.fsprg_secpar = htole16(FSPRG_RECOMMENDED_SECPAR), |
| 151 |
.fsprg_state_size = htole64(state_size), |
| 152 |
}; |
| 153 |
|
| 154 |
r = loop_write(fd, &h, sizeof(h)); |
| 155 |
if (r < 0) |
| 156 |
return log_error_errno(r, "Failed to write header: %m"); |
| 157 |
|
| 158 |
r = loop_write(fd, state, state_size); |
| 159 |
if (r < 0) |
| 160 |
return log_error_errno(r, "Failed to write state: %m"); |
| 161 |
|
| 162 |
r = link_tmpfile(fd, tmpfile, path, /* flags= */ 0); |
| 163 |
if (r < 0) |
| 164 |
return log_error_errno(r, "Failed to link file: %m"); |
| 165 |
|
| 166 |
tmpfile = mfree(tmpfile); |
| 167 |
|
| 168 |
_cleanup_free_ char *key = NULL; |
| 169 |
r = format_key(seed, seed_size, n, arg_interval, &key); |
| 170 |
if (r < 0) |
| 171 |
return r; |
| 172 |
|
| 173 |
if ((!on_tty() || arg_quiet) && !sd_json_format_enabled(arg_json_format_flags)) { |
| 174 |
/* If we are not on a TTY, show only the key. */ |
| 175 |
puts(key); |
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
_cleanup_free_ char *hn = NULL; |
| 180 |
hn = gethostname_malloc(); |
| 181 |
if (hn) |
| 182 |
hostname_cleanup(hn); |
| 183 |
|
| 184 |
if (sd_json_format_enabled(arg_json_format_flags)) { |
| 185 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 186 |
|
| 187 |
if (arg_json_format_flags & (SD_JSON_FORMAT_SSE | SD_JSON_FORMAT_SEQ)) { |
| 188 |
log_debug("Specified --output=%s with --setup-keys, migrating to --output=json.", |
| 189 |
FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_SSE) ? "json-sse" : "json-seq"); |
| 190 |
arg_json_format_flags &= ~(SD_JSON_FORMAT_SSE | SD_JSON_FORMAT_SEQ); |
| 191 |
arg_json_format_flags |= SD_JSON_FORMAT_NEWLINE; |
| 192 |
} |
| 193 |
|
| 194 |
r = sd_json_buildo( |
| 195 |
&v, |
| 196 |
SD_JSON_BUILD_PAIR_ID128("machine", machine), |
| 197 |
SD_JSON_BUILD_PAIR_STRING("hostname", hn), |
| 198 |
SD_JSON_BUILD_PAIR_STRING("path", path), |
| 199 |
SD_JSON_BUILD_PAIR_STRING("key", key)); |
| 200 |
if (r < 0) |
| 201 |
return log_error_errno(r, "Failed to build json object: %m"); |
| 202 |
|
| 203 |
r = sd_json_variant_dump(v, arg_json_format_flags, /* f= */ NULL, /* prefix= */ NULL); |
| 204 |
if (r < 0) |
| 205 |
return log_error_errno(r, "Failed to dump json object: %m"); |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
fprintf(stderr, |
| 211 |
"\nNew keys have been generated for host %s%s" SD_ID128_FORMAT_STR ".\n" |
| 212 |
"\n" |
| 213 |
"The %ssecret sealing key%s has been written to the following local file.\n" |
| 214 |
"This key file is automatically updated when the sealing key is advanced.\n" |
| 215 |
"It should not be used on multiple hosts.\n" |
| 216 |
"\n" |
| 217 |
"\t%s\n" |
| 218 |
"\n" |
| 219 |
"The sealing key is automatically changed every %s.\n" |
| 220 |
"\n" |
| 221 |
"Please write down the following %ssecret verification key%s. It should be stored\n" |
| 222 |
"in a safe location and should not be saved locally on disk.\n" |
| 223 |
"\n\t%s", |
| 224 |
strempty(hn), hn ? "/" : "", |
| 225 |
SD_ID128_FORMAT_VAL(machine), |
| 226 |
ansi_highlight(), ansi_normal(), |
| 227 |
path, |
| 228 |
FORMAT_TIMESPAN(arg_interval, 0), |
| 229 |
ansi_highlight(), ansi_normal(), |
| 230 |
ansi_highlight_red()); |
| 231 |
fflush(stderr); |
| 232 |
|
| 233 |
puts(key); |
| 234 |
|
| 235 |
fputs(ansi_normal(), stderr); |
| 236 |
|
| 237 |
#if HAVE_QRENCODE |
| 238 |
_cleanup_free_ char *url = NULL; |
| 239 |
url = strjoin("fss://", key, "?machine=", SD_ID128_TO_STRING(machine), hn ? ";hostname=" : "", hn); |
| 240 |
if (!url) |
| 241 |
return log_oom(); |
| 242 |
|
| 243 |
(void) print_qrcode(stderr, "Scan the verification key to transfer it to another device", url); |
| 244 |
#endif |
| 245 |
|
| 246 |
return 0; |
| 247 |
#else |
| 248 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Forward-secure sealing not available."); |
| 249 |
#endif |
| 250 |
} |