| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "fido2-util.h" |
| 7 |
#include "fileio.h" |
| 8 |
#include "iovec-util.h" |
| 9 |
#include "libfido2-util.h" |
| 10 |
#include "log.h" |
| 11 |
#include "random-util.h" |
| 12 |
|
| 13 |
int fido2_generate_salt(struct iovec *ret_salt) { |
| 14 |
_cleanup_(iovec_done) struct iovec salt = {}; |
| 15 |
int r; |
| 16 |
|
| 17 |
assert(ret_salt); |
| 18 |
|
| 19 |
r = crypto_random_bytes_allocate_iovec(FIDO2_SALT_SIZE, &salt); |
| 20 |
if (r < 0) |
| 21 |
return log_error_errno(r, "Failed to generate FIDO2 salt: %m"); |
| 22 |
|
| 23 |
*ret_salt = TAKE_STRUCT(salt); |
| 24 |
return 0; |
| 25 |
} |
| 26 |
|
| 27 |
int fido2_read_salt_file(const char *filename, uint64_t offset, const char *client, const char *node, struct iovec *ret_salt) { |
| 28 |
_cleanup_(iovec_done_erase) struct iovec salt = {}; |
| 29 |
_cleanup_free_ char *bind_name = NULL; |
| 30 |
int r; |
| 31 |
|
| 32 |
assert(ret_salt); |
| 33 |
|
| 34 |
/* If we read the salt via AF_UNIX, make the client recognizable */ |
| 35 |
if (asprintf(&bind_name, "@%" PRIx64"/%s-fido2-salt/%s", random_u64(), client, node) < 0) |
| 36 |
return log_oom(); |
| 37 |
|
| 38 |
r = read_full_file_full( |
| 39 |
AT_FDCWD, filename, |
| 40 |
offset == 0 ? UINT64_MAX : offset, |
| 41 |
/* size= */ FIDO2_SALT_SIZE, |
| 42 |
READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE| |
| 43 |
READ_FULL_FILE_CONNECT_SOCKET|READ_FULL_FILE_FAIL_WHEN_LARGER, |
| 44 |
bind_name, (char**) &salt.iov_base, &salt.iov_len); |
| 45 |
if (r == -E2BIG || (r >= 0 && salt.iov_len != FIDO2_SALT_SIZE)) |
| 46 |
return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL), |
| 47 |
"FIDO2 salt file must contain exactly %u bytes.", FIDO2_SALT_SIZE); |
| 48 |
if (r < 0) |
| 49 |
return log_error_errno(r, "Reading FIDO2 salt file '%s' failed: %m", filename); |
| 50 |
|
| 51 |
*ret_salt = TAKE_STRUCT(salt); |
| 52 |
return 0; |
| 53 |
} |