| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "ask-password-api.h" |
| 7 |
#include "env-util.h" |
| 8 |
#include "hexdecoct.h" |
| 9 |
#include "log.h" |
| 10 |
#include "luks2-tpm2.h" |
| 11 |
#include "parse-util.h" |
| 12 |
#include "random-util.h" |
| 13 |
#include "sha256.h" |
| 14 |
#include "strv.h" |
| 15 |
#include "tpm2-util.h" |
| 16 |
|
| 17 |
int acquire_luks2_key( |
| 18 |
const char *device, |
| 19 |
uint32_t hash_pcr_mask, |
| 20 |
uint16_t pcr_bank, |
| 21 |
const struct iovec *pubkey, |
| 22 |
uint32_t pubkey_pcr_mask, |
| 23 |
const char *signature_path, |
| 24 |
const char *pin, |
| 25 |
const char *pcrlock_path, |
| 26 |
uint16_t primary_alg, |
| 27 |
const struct iovec blobs[], |
| 28 |
size_t n_blobs, |
| 29 |
const struct iovec policy_hash[], |
| 30 |
size_t n_policy_hash, |
| 31 |
const struct iovec *salt, |
| 32 |
const struct iovec *srk, |
| 33 |
const struct iovec *pcrlock_nv, |
| 34 |
TPM2Flags flags, |
| 35 |
struct iovec *ret_decrypted_key) { |
| 36 |
|
| 37 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *signature_json = NULL; |
| 38 |
_cleanup_free_ char *auto_device = NULL; |
| 39 |
_cleanup_(erase_and_freep) char *b64_salted_pin = NULL; |
| 40 |
int r; |
| 41 |
|
| 42 |
assert(iovec_is_valid(salt)); |
| 43 |
assert(ret_decrypted_key); |
| 44 |
|
| 45 |
if (!device) { |
| 46 |
r = tpm2_find_device_auto(&auto_device); |
| 47 |
if (r == -ENODEV) |
| 48 |
return -EAGAIN; /* Tell the caller to wait for a TPM2 device to show up */ |
| 49 |
if (r < 0) |
| 50 |
return log_error_errno(r, "Could not find TPM2 device: %m"); |
| 51 |
|
| 52 |
device = auto_device; |
| 53 |
} |
| 54 |
|
| 55 |
if ((flags & TPM2_FLAGS_USE_PIN) && !pin) |
| 56 |
return -ENOANO; |
| 57 |
|
| 58 |
if (pin && iovec_is_set(salt)) { |
| 59 |
uint8_t salted_pin[SHA256_DIGEST_SIZE] = {}; |
| 60 |
CLEANUP_ERASE(salted_pin); |
| 61 |
r = tpm2_util_pbkdf2_hmac_sha256(pin, strlen(pin), salt->iov_base, salt->iov_len, salted_pin); |
| 62 |
if (r < 0) |
| 63 |
return log_error_errno(r, "Failed to perform PBKDF2: %m"); |
| 64 |
|
| 65 |
r = base64mem(salted_pin, sizeof(salted_pin), &b64_salted_pin); |
| 66 |
if (r < 0) |
| 67 |
return log_error_errno(r, "Failed to base64 encode salted pin: %m"); |
| 68 |
pin = b64_salted_pin; |
| 69 |
} |
| 70 |
|
| 71 |
if (pubkey_pcr_mask != 0) { |
| 72 |
r = tpm2_load_pcr_signature(signature_path, &signature_json); |
| 73 |
if (r < 0) |
| 74 |
return log_error_errno(r, "Failed to load PCR signature: %m"); |
| 75 |
} |
| 76 |
|
| 77 |
_cleanup_(tpm2_pcrlock_policy_done) Tpm2PCRLockPolicy pcrlock_policy = {}; |
| 78 |
if (FLAGS_SET(flags, TPM2_FLAGS_USE_PCRLOCK)) { |
| 79 |
r = tpm2_pcrlock_policy_load(pcrlock_path, &pcrlock_policy); |
| 80 |
if (r < 0) |
| 81 |
return r; |
| 82 |
if (r == 0) { |
| 83 |
/* Not found? Then search among passed credentials */ |
| 84 |
r = tpm2_pcrlock_policy_from_credentials(srk, pcrlock_nv, &pcrlock_policy); |
| 85 |
if (r < 0) |
| 86 |
return r; |
| 87 |
if (r == 0) |
| 88 |
return log_error_errno(SYNTHETIC_ERRNO(EREMOTE), "Couldn't find pcrlock policy for volume."); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
_cleanup_(tpm2_context_unrefp) Tpm2Context *tpm2_context = NULL; |
| 93 |
r = tpm2_context_new_or_warn(device, &tpm2_context); |
| 94 |
if (r < 0) |
| 95 |
return r; |
| 96 |
|
| 97 |
r = tpm2_unseal(tpm2_context, |
| 98 |
hash_pcr_mask, |
| 99 |
pcr_bank, |
| 100 |
pubkey, |
| 101 |
pubkey_pcr_mask, |
| 102 |
signature_json, |
| 103 |
pin, |
| 104 |
FLAGS_SET(flags, TPM2_FLAGS_USE_PCRLOCK) ? &pcrlock_policy : NULL, |
| 105 |
primary_alg, |
| 106 |
blobs, |
| 107 |
n_blobs, |
| 108 |
policy_hash, |
| 109 |
n_policy_hash, |
| 110 |
srk, |
| 111 |
ret_decrypted_key); |
| 112 |
if (r == -EREMOTE) |
| 113 |
return log_error_errno(r, "TPM key integrity check failed. Key enrolled in superblock most likely does not belong to this TPM."); |
| 114 |
if (r == -EILSEQ) |
| 115 |
return log_error_errno(SYNTHETIC_ERRNO(ENOANO), "Bad PIN."); /* cryptsetup docs say we should return ENOANO on bad PIN */ |
| 116 |
if (r == -ENOLCK) |
| 117 |
return log_error_errno(r, "TPM is in dictionary attack lock-out mode."); |
| 118 |
if (ERRNO_IS_NEG_TPM2_UNSEAL_BAD_PCR(r)) |
| 119 |
return log_error_errno(r, "TPM policy does not match current system state. Either system has been tempered with or policy out-of-date: %m"); |
| 120 |
if (r < 0) |
| 121 |
return log_error_errno(r, "Failed to unseal secret using TPM2: %m"); |
| 122 |
|
| 123 |
return r; |
| 124 |
} |