tpm2-util: more iovec'ification
Let's move more code to using struct iovec for passing around binary chunks of data. No real changes in behaviour, just refactoring.
This commit is contained in:
@@ -42,9 +42,8 @@ _public_ int cryptsetup_token_open_pin(
|
||||
void *usrptr /* plugin defined parameter passed to crypt_activate_by_token*() API */) {
|
||||
|
||||
_cleanup_(erase_and_freep) char *base64_encoded = NULL, *pin_string = NULL;
|
||||
_cleanup_free_ void *blob = NULL, *pubkey = NULL, *policy_hash = NULL, *salt = NULL, *srk_buf = NULL;
|
||||
size_t blob_size, policy_hash_size, decrypted_key_size, pubkey_size, salt_size = 0, srk_buf_size = 0;
|
||||
_cleanup_(erase_and_freep) void *decrypted_key = NULL;
|
||||
_cleanup_(iovec_done) struct iovec blob = {}, pubkey = {}, policy_hash = {}, salt = {}, srk = {};
|
||||
_cleanup_(iovec_done_erase) struct iovec decrypted_key = {};
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
|
||||
uint32_t hash_pcr_mask, pubkey_pcr_mask;
|
||||
systemd_tpm2_plugin_params params = {
|
||||
@@ -79,21 +78,16 @@ _public_ int cryptsetup_token_open_pin(
|
||||
|
||||
r = tpm2_parse_luks2_json(
|
||||
v,
|
||||
NULL,
|
||||
/* ret_keyslot= */ NULL,
|
||||
&hash_pcr_mask,
|
||||
&pcr_bank,
|
||||
&pubkey,
|
||||
&pubkey_size,
|
||||
&pubkey_pcr_mask,
|
||||
&primary_alg,
|
||||
&blob,
|
||||
&blob_size,
|
||||
&policy_hash,
|
||||
&policy_hash_size,
|
||||
&salt,
|
||||
&salt_size,
|
||||
&srk_buf,
|
||||
&srk_buf_size,
|
||||
&srk,
|
||||
&flags);
|
||||
if (r < 0)
|
||||
return log_debug_open_error(cd, r);
|
||||
@@ -105,28 +99,23 @@ _public_ int cryptsetup_token_open_pin(
|
||||
params.device,
|
||||
hash_pcr_mask,
|
||||
pcr_bank,
|
||||
pubkey, pubkey_size,
|
||||
&pubkey,
|
||||
pubkey_pcr_mask,
|
||||
params.signature_path,
|
||||
pin_string,
|
||||
params.pcrlock_path,
|
||||
primary_alg,
|
||||
blob,
|
||||
blob_size,
|
||||
policy_hash,
|
||||
policy_hash_size,
|
||||
salt,
|
||||
salt_size,
|
||||
srk_buf,
|
||||
srk_buf_size,
|
||||
&blob,
|
||||
&policy_hash,
|
||||
&salt,
|
||||
&srk,
|
||||
flags,
|
||||
&decrypted_key,
|
||||
&decrypted_key_size);
|
||||
&decrypted_key);
|
||||
if (r < 0)
|
||||
return log_debug_open_error(cd, r);
|
||||
|
||||
/* Before using this key as passphrase we base64 encode it, for compat with homed */
|
||||
base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
|
||||
base64_encoded_size = base64mem(decrypted_key.iov_base, decrypted_key.iov_len, &base64_encoded);
|
||||
if (base64_encoded_size < 0)
|
||||
return log_debug_open_error(cd, base64_encoded_size);
|
||||
|
||||
@@ -177,9 +166,8 @@ _public_ void cryptsetup_token_dump(
|
||||
const char *json /* validated 'systemd-tpm2' token if cryptsetup_token_validate is defined */) {
|
||||
|
||||
_cleanup_free_ char *hash_pcrs_str = NULL, *pubkey_pcrs_str = NULL, *blob_str = NULL, *policy_hash_str = NULL, *pubkey_str = NULL;
|
||||
_cleanup_free_ void *blob = NULL, *pubkey = NULL, *policy_hash = NULL, *salt = NULL, *srk_buf = NULL;
|
||||
_cleanup_(iovec_done) struct iovec blob = {}, pubkey = {}, policy_hash = {}, salt = {}, srk = {};
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
|
||||
size_t blob_size, policy_hash_size, pubkey_size, salt_size = 0, srk_buf_size = 0;
|
||||
uint32_t hash_pcr_mask, pubkey_pcr_mask;
|
||||
uint16_t pcr_bank, primary_alg;
|
||||
TPM2Flags flags = 0;
|
||||
@@ -197,17 +185,12 @@ _public_ void cryptsetup_token_dump(
|
||||
&hash_pcr_mask,
|
||||
&pcr_bank,
|
||||
&pubkey,
|
||||
&pubkey_size,
|
||||
&pubkey_pcr_mask,
|
||||
&primary_alg,
|
||||
&blob,
|
||||
&blob_size,
|
||||
&policy_hash,
|
||||
&policy_hash_size,
|
||||
&salt,
|
||||
&salt_size,
|
||||
&srk_buf,
|
||||
&srk_buf_size,
|
||||
&srk,
|
||||
&flags);
|
||||
if (r < 0)
|
||||
return (void) crypt_log_debug_errno(cd, r, "Failed to parse " TOKEN_NAME " JSON fields: %m");
|
||||
@@ -220,15 +203,15 @@ _public_ void cryptsetup_token_dump(
|
||||
if (!pubkey_pcrs_str)
|
||||
return (void) crypt_log_debug_errno(cd, ENOMEM, "Cannot format PCR hash mask: %m");
|
||||
|
||||
r = crypt_dump_buffer_to_hex_string(blob, blob_size, &blob_str);
|
||||
r = crypt_dump_buffer_to_hex_string(blob.iov_base, blob.iov_len, &blob_str);
|
||||
if (r < 0)
|
||||
return (void) crypt_log_debug_errno(cd, r, "Cannot dump " TOKEN_NAME " content: %m");
|
||||
|
||||
r = crypt_dump_buffer_to_hex_string(pubkey, pubkey_size, &pubkey_str);
|
||||
r = crypt_dump_buffer_to_hex_string(pubkey.iov_base, pubkey.iov_len, &pubkey_str);
|
||||
if (r < 0)
|
||||
return (void) crypt_log_debug_errno(cd, r, "Cannot dump " TOKEN_NAME " content: %m");
|
||||
|
||||
r = crypt_dump_buffer_to_hex_string(policy_hash, policy_hash_size, &policy_hash_str);
|
||||
r = crypt_dump_buffer_to_hex_string(policy_hash.iov_base, policy_hash.iov_len, &policy_hash_str);
|
||||
if (r < 0)
|
||||
return (void) crypt_log_debug_errno(cd, r, "Cannot dump " TOKEN_NAME " content: %m");
|
||||
|
||||
@@ -241,8 +224,8 @@ _public_ void cryptsetup_token_dump(
|
||||
crypt_log(cd, "\ttpm2-policy-hash:" CRYPT_DUMP_LINE_SEP "%s\n", policy_hash_str);
|
||||
crypt_log(cd, "\ttpm2-pin: %s\n", true_false(flags & TPM2_FLAGS_USE_PIN));
|
||||
crypt_log(cd, "\ttpm2-pcrlock: %s\n", true_false(flags & TPM2_FLAGS_USE_PCRLOCK));
|
||||
crypt_log(cd, "\ttpm2-salt: %s\n", true_false(salt));
|
||||
crypt_log(cd, "\ttpm2-srk: %s\n", true_false(srk_buf));
|
||||
crypt_log(cd, "\ttpm2-salt: %s\n", true_false(iovec_is_set(&salt)));
|
||||
crypt_log(cd, "\ttpm2-srk: %s\n", true_false(iovec_is_set(&srk)));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -17,33 +17,26 @@ int acquire_luks2_key(
|
||||
const char *device,
|
||||
uint32_t hash_pcr_mask,
|
||||
uint16_t pcr_bank,
|
||||
const void *pubkey,
|
||||
size_t pubkey_size,
|
||||
const struct iovec *pubkey,
|
||||
uint32_t pubkey_pcr_mask,
|
||||
const char *signature_path,
|
||||
const char *pin,
|
||||
const char *pcrlock_path,
|
||||
uint16_t primary_alg,
|
||||
const void *key_data,
|
||||
size_t key_data_size,
|
||||
const void *policy_hash,
|
||||
size_t policy_hash_size,
|
||||
const void *salt,
|
||||
size_t salt_size,
|
||||
const void *srk_buf,
|
||||
size_t srk_buf_size,
|
||||
const struct iovec *blob,
|
||||
const struct iovec *policy_hash,
|
||||
const struct iovec *salt,
|
||||
const struct iovec *srk,
|
||||
TPM2Flags flags,
|
||||
void **ret_decrypted_key,
|
||||
size_t *ret_decrypted_key_size) {
|
||||
struct iovec *ret_decrypted_key) {
|
||||
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *signature_json = NULL;
|
||||
_cleanup_free_ char *auto_device = NULL;
|
||||
_cleanup_(erase_and_freep) char *b64_salted_pin = NULL;
|
||||
int r;
|
||||
|
||||
assert(salt || salt_size == 0);
|
||||
assert(iovec_is_valid(salt));
|
||||
assert(ret_decrypted_key);
|
||||
assert(ret_decrypted_key_size);
|
||||
|
||||
if (!device) {
|
||||
r = tpm2_find_device_auto(&auto_device);
|
||||
@@ -58,10 +51,10 @@ int acquire_luks2_key(
|
||||
if ((flags & TPM2_FLAGS_USE_PIN) && !pin)
|
||||
return -ENOANO;
|
||||
|
||||
if (pin && salt_size > 0) {
|
||||
if (pin && iovec_is_set(salt)) {
|
||||
uint8_t salted_pin[SHA256_DIGEST_SIZE] = {};
|
||||
CLEANUP_ERASE(salted_pin);
|
||||
r = tpm2_util_pbkdf2_hmac_sha256(pin, strlen(pin), salt, salt_size, salted_pin);
|
||||
r = tpm2_util_pbkdf2_hmac_sha256(pin, strlen(pin), salt->iov_base, salt->iov_len, salted_pin);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to perform PBKDF2: %m");
|
||||
|
||||
@@ -92,16 +85,16 @@ int acquire_luks2_key(
|
||||
r = tpm2_unseal(tpm2_context,
|
||||
hash_pcr_mask,
|
||||
pcr_bank,
|
||||
pubkey, pubkey_size,
|
||||
pubkey,
|
||||
pubkey_pcr_mask,
|
||||
signature_json,
|
||||
pin,
|
||||
FLAGS_SET(flags, TPM2_FLAGS_USE_PCRLOCK) ? &pcrlock_policy : NULL,
|
||||
primary_alg,
|
||||
key_data, key_data_size,
|
||||
policy_hash, policy_hash_size,
|
||||
srk_buf, srk_buf_size,
|
||||
ret_decrypted_key, ret_decrypted_key_size);
|
||||
blob,
|
||||
policy_hash,
|
||||
srk,
|
||||
ret_decrypted_key);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to unseal secret using TPM2: %m");
|
||||
|
||||
|
||||
@@ -10,21 +10,15 @@ int acquire_luks2_key(
|
||||
const char *device,
|
||||
uint32_t pcr_mask,
|
||||
uint16_t pcr_bank,
|
||||
const void *pubkey,
|
||||
size_t pubkey_size,
|
||||
const struct iovec *pubkey,
|
||||
uint32_t pubkey_pcr_mask,
|
||||
const char *signature_path,
|
||||
const char *pcrlock_path,
|
||||
const char *pin,
|
||||
uint16_t primary_alg,
|
||||
const void *key_data,
|
||||
size_t key_data_size,
|
||||
const void *policy_hash,
|
||||
size_t policy_hash_size,
|
||||
const void *salt,
|
||||
size_t salt_size,
|
||||
const void *srk_buf,
|
||||
size_t srk_buf_size,
|
||||
const struct iovec *key_data,
|
||||
const struct iovec *policy_hash,
|
||||
const struct iovec *salt,
|
||||
const struct iovec *srk,
|
||||
TPM2Flags flags,
|
||||
void **ret_decrypted_key,
|
||||
size_t *ret_decrypted_key_size);
|
||||
struct iovec *decrypted_key);
|
||||
|
||||
@@ -58,8 +58,7 @@ int acquire_tpm2_key(
|
||||
const char *device,
|
||||
uint32_t hash_pcr_mask,
|
||||
uint16_t pcr_bank,
|
||||
const void *pubkey,
|
||||
size_t pubkey_size,
|
||||
const struct iovec *pubkey,
|
||||
uint32_t pubkey_pcr_mask,
|
||||
const char *signature_path,
|
||||
const char *pcrlock_path,
|
||||
@@ -67,29 +66,23 @@ int acquire_tpm2_key(
|
||||
const char *key_file,
|
||||
size_t key_file_size,
|
||||
uint64_t key_file_offset,
|
||||
const void *key_data,
|
||||
size_t key_data_size,
|
||||
const void *policy_hash,
|
||||
size_t policy_hash_size,
|
||||
const void *salt,
|
||||
size_t salt_size,
|
||||
const void *srk_buf,
|
||||
size_t srk_buf_size,
|
||||
const struct iovec *key_data,
|
||||
const struct iovec *policy_hash,
|
||||
const struct iovec *salt,
|
||||
const struct iovec *srk,
|
||||
TPM2Flags flags,
|
||||
usec_t until,
|
||||
bool headless,
|
||||
AskPasswordFlags ask_password_flags,
|
||||
void **ret_decrypted_key,
|
||||
size_t *ret_decrypted_key_size) {
|
||||
struct iovec *ret_decrypted_key) {
|
||||
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *signature_json = NULL;
|
||||
_cleanup_free_ void *loaded_blob = NULL;
|
||||
_cleanup_free_ char *auto_device = NULL;
|
||||
size_t blob_size;
|
||||
const void *blob;
|
||||
struct iovec blob;
|
||||
int r;
|
||||
|
||||
assert(salt || salt_size == 0);
|
||||
assert(iovec_is_valid(salt));
|
||||
|
||||
if (!device) {
|
||||
r = tpm2_find_device_auto(&auto_device);
|
||||
@@ -101,10 +94,9 @@ int acquire_tpm2_key(
|
||||
device = auto_device;
|
||||
}
|
||||
|
||||
if (key_data) {
|
||||
blob = key_data;
|
||||
blob_size = key_data_size;
|
||||
} else {
|
||||
if (iovec_is_set(key_data))
|
||||
blob = *key_data;
|
||||
else {
|
||||
_cleanup_free_ char *bindname = NULL;
|
||||
|
||||
/* If we read the salt via AF_UNIX, make this client recognizable */
|
||||
@@ -117,11 +109,11 @@ int acquire_tpm2_key(
|
||||
key_file_size == 0 ? SIZE_MAX : key_file_size,
|
||||
READ_FULL_FILE_CONNECT_SOCKET,
|
||||
bindname,
|
||||
(char**) &loaded_blob, &blob_size);
|
||||
(char**) &loaded_blob, &blob.iov_len);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
blob = loaded_blob;
|
||||
blob.iov_base = loaded_blob;
|
||||
}
|
||||
|
||||
if (pubkey_pcr_mask != 0) {
|
||||
@@ -147,20 +139,16 @@ int acquire_tpm2_key(
|
||||
r = tpm2_unseal(tpm2_context,
|
||||
hash_pcr_mask,
|
||||
pcr_bank,
|
||||
pubkey, pubkey_size,
|
||||
pubkey,
|
||||
pubkey_pcr_mask,
|
||||
signature_json,
|
||||
/* pin= */ NULL,
|
||||
FLAGS_SET(flags, TPM2_FLAGS_USE_PCRLOCK) ? &pcrlock_policy : NULL,
|
||||
primary_alg,
|
||||
blob,
|
||||
blob_size,
|
||||
&blob,
|
||||
policy_hash,
|
||||
policy_hash_size,
|
||||
srk_buf,
|
||||
srk_buf_size,
|
||||
ret_decrypted_key,
|
||||
ret_decrypted_key_size);
|
||||
srk,
|
||||
ret_decrypted_key);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to unseal secret using TPM2: %m");
|
||||
|
||||
@@ -177,11 +165,11 @@ int acquire_tpm2_key(
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (salt_size > 0) {
|
||||
if (iovec_is_set(salt)) {
|
||||
uint8_t salted_pin[SHA256_DIGEST_SIZE] = {};
|
||||
CLEANUP_ERASE(salted_pin);
|
||||
|
||||
r = tpm2_util_pbkdf2_hmac_sha256(pin_str, strlen(pin_str), salt, salt_size, salted_pin);
|
||||
r = tpm2_util_pbkdf2_hmac_sha256(pin_str, strlen(pin_str), salt->iov_base, salt->iov_len, salted_pin);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to perform PBKDF2: %m");
|
||||
|
||||
@@ -195,20 +183,16 @@ int acquire_tpm2_key(
|
||||
r = tpm2_unseal(tpm2_context,
|
||||
hash_pcr_mask,
|
||||
pcr_bank,
|
||||
pubkey, pubkey_size,
|
||||
pubkey,
|
||||
pubkey_pcr_mask,
|
||||
signature_json,
|
||||
b64_salted_pin,
|
||||
pcrlock_path ? &pcrlock_policy : NULL,
|
||||
primary_alg,
|
||||
blob,
|
||||
blob_size,
|
||||
&blob,
|
||||
policy_hash,
|
||||
policy_hash_size,
|
||||
srk_buf,
|
||||
srk_buf_size,
|
||||
ret_decrypted_key,
|
||||
ret_decrypted_key_size);
|
||||
srk,
|
||||
ret_decrypted_key);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to unseal secret using TPM2: %m");
|
||||
|
||||
@@ -228,18 +212,13 @@ int find_tpm2_auto_data(
|
||||
int start_token,
|
||||
uint32_t *ret_hash_pcr_mask,
|
||||
uint16_t *ret_pcr_bank,
|
||||
void **ret_pubkey,
|
||||
size_t *ret_pubkey_size,
|
||||
struct iovec *ret_pubkey,
|
||||
uint32_t *ret_pubkey_pcr_mask,
|
||||
uint16_t *ret_primary_alg,
|
||||
void **ret_blob,
|
||||
size_t *ret_blob_size,
|
||||
void **ret_policy_hash,
|
||||
size_t *ret_policy_hash_size,
|
||||
void **ret_salt,
|
||||
size_t *ret_salt_size,
|
||||
void **ret_srk_buf,
|
||||
size_t *ret_srk_buf_size,
|
||||
struct iovec *ret_blob,
|
||||
struct iovec *ret_policy_hash,
|
||||
struct iovec *ret_salt,
|
||||
struct iovec *ret_srk,
|
||||
TPM2Flags *ret_flags,
|
||||
int *ret_keyslot,
|
||||
int *ret_token) {
|
||||
@@ -249,9 +228,8 @@ int find_tpm2_auto_data(
|
||||
assert(cd);
|
||||
|
||||
for (token = start_token; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
|
||||
_cleanup_free_ void *blob = NULL, *policy_hash = NULL, *pubkey = NULL, *salt = NULL, *srk_buf = NULL;
|
||||
_cleanup_(iovec_done) struct iovec blob = {}, policy_hash = {}, pubkey = {}, salt = {}, srk = {};
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
|
||||
size_t blob_size, policy_hash_size, pubkey_size, salt_size = 0, srk_buf_size = 0;
|
||||
uint32_t hash_pcr_mask, pubkey_pcr_mask;
|
||||
uint16_t pcr_bank, primary_alg;
|
||||
TPM2Flags flags;
|
||||
@@ -268,13 +246,13 @@ int find_tpm2_auto_data(
|
||||
&keyslot,
|
||||
&hash_pcr_mask,
|
||||
&pcr_bank,
|
||||
&pubkey, &pubkey_size,
|
||||
&pubkey,
|
||||
&pubkey_pcr_mask,
|
||||
&primary_alg,
|
||||
&blob, &blob_size,
|
||||
&policy_hash, &policy_hash_size,
|
||||
&salt, &salt_size,
|
||||
&srk_buf, &srk_buf_size,
|
||||
&blob,
|
||||
&policy_hash,
|
||||
&salt,
|
||||
&srk,
|
||||
&flags);
|
||||
if (r == -EUCLEAN) /* Gracefully handle issues in JSON fields not owned by us */
|
||||
continue;
|
||||
@@ -289,20 +267,15 @@ int find_tpm2_auto_data(
|
||||
|
||||
*ret_hash_pcr_mask = hash_pcr_mask;
|
||||
*ret_pcr_bank = pcr_bank;
|
||||
*ret_pubkey = TAKE_PTR(pubkey);
|
||||
*ret_pubkey_size = pubkey_size;
|
||||
*ret_pubkey = TAKE_STRUCT(pubkey);
|
||||
*ret_pubkey_pcr_mask = pubkey_pcr_mask;
|
||||
*ret_primary_alg = primary_alg;
|
||||
*ret_blob = TAKE_PTR(blob);
|
||||
*ret_blob_size = blob_size;
|
||||
*ret_policy_hash = TAKE_PTR(policy_hash);
|
||||
*ret_policy_hash_size = policy_hash_size;
|
||||
*ret_salt = TAKE_PTR(salt);
|
||||
*ret_salt_size = salt_size;
|
||||
*ret_blob = TAKE_STRUCT(blob);
|
||||
*ret_policy_hash = TAKE_STRUCT(policy_hash);
|
||||
*ret_salt = TAKE_STRUCT(salt);
|
||||
*ret_keyslot = keyslot;
|
||||
*ret_token = token;
|
||||
*ret_srk_buf = TAKE_PTR(srk_buf);
|
||||
*ret_srk_buf_size = srk_buf_size;
|
||||
*ret_srk = TAKE_STRUCT(srk);
|
||||
*ret_flags = flags;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,7 @@ int acquire_tpm2_key(
|
||||
const char *device,
|
||||
uint32_t hash_pcr_mask,
|
||||
uint16_t pcr_bank,
|
||||
const void *pubkey,
|
||||
size_t pubkey_size,
|
||||
const struct iovec *pubkey,
|
||||
uint32_t pubkey_pcr_mask,
|
||||
const char *signature_path,
|
||||
const char *pcrlock_path,
|
||||
@@ -25,20 +24,15 @@ int acquire_tpm2_key(
|
||||
const char *key_file,
|
||||
size_t key_file_size,
|
||||
uint64_t key_file_offset,
|
||||
const void *key_data,
|
||||
size_t key_data_size,
|
||||
const void *policy_hash,
|
||||
size_t policy_hash_size,
|
||||
const void *salt,
|
||||
size_t salt_size,
|
||||
const void *srk_buf,
|
||||
size_t salt_srk_buf_size,
|
||||
const struct iovec *key_data,
|
||||
const struct iovec *policy_hash,
|
||||
const struct iovec *salt,
|
||||
const struct iovec *srk,
|
||||
TPM2Flags flags,
|
||||
usec_t until,
|
||||
bool headless,
|
||||
AskPasswordFlags ask_password_flags,
|
||||
void **ret_decrypted_key,
|
||||
size_t *ret_decrypted_key_size);
|
||||
struct iovec *ret_decrypted_key);
|
||||
|
||||
int find_tpm2_auto_data(
|
||||
struct crypt_device *cd,
|
||||
@@ -46,18 +40,13 @@ int find_tpm2_auto_data(
|
||||
int start_token,
|
||||
uint32_t *ret_hash_pcr_mask,
|
||||
uint16_t *ret_pcr_bank,
|
||||
void **ret_pubkey,
|
||||
size_t *ret_pubkey_size,
|
||||
struct iovec *ret_pubkey,
|
||||
uint32_t *ret_pubkey_pcr_mask,
|
||||
uint16_t *ret_primary_alg,
|
||||
void **ret_blob,
|
||||
size_t *ret_blob_size,
|
||||
void **ret_policy_hash,
|
||||
size_t *ret_policy_hash_size,
|
||||
void **ret_salt,
|
||||
size_t *ret_salt_size,
|
||||
void **ret_srk_buf,
|
||||
size_t *ret_srk_size,
|
||||
struct iovec *ret_blob,
|
||||
struct iovec *ret_policy_hash,
|
||||
struct iovec *ret_salt,
|
||||
struct iovec *ret_srk,
|
||||
TPM2Flags *ret_flags,
|
||||
int *ret_keyslot,
|
||||
int *ret_token);
|
||||
@@ -69,8 +58,7 @@ static inline int acquire_tpm2_key(
|
||||
const char *device,
|
||||
uint32_t hash_pcr_mask,
|
||||
uint16_t pcr_bank,
|
||||
const void *pubkey,
|
||||
size_t pubkey_size,
|
||||
const struct iovec *pubkey,
|
||||
uint32_t pubkey_pcr_mask,
|
||||
const char *signature_path,
|
||||
const char *pcrlock_path,
|
||||
@@ -78,20 +66,15 @@ static inline int acquire_tpm2_key(
|
||||
const char *key_file,
|
||||
size_t key_file_size,
|
||||
uint64_t key_file_offset,
|
||||
const void *key_data,
|
||||
size_t key_data_size,
|
||||
const void *policy_hash,
|
||||
size_t policy_hash_size,
|
||||
const void *salt,
|
||||
size_t salt_size,
|
||||
const void *srk_buf,
|
||||
size_t salt_srk_buf_size,
|
||||
const struct iovec *key_data,
|
||||
const struct iovec *policy_hash,
|
||||
const struct iovec *salt,
|
||||
const struct iovec *srk,
|
||||
TPM2Flags flags,
|
||||
usec_t until,
|
||||
bool headless,
|
||||
AskPasswordFlags ask_password_flags,
|
||||
void **ret_decrypted_key,
|
||||
size_t *ret_decrypted_key_size) {
|
||||
struct iovec *ret_decrypted_key) {
|
||||
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
|
||||
"TPM2 support not available.");
|
||||
@@ -103,18 +86,13 @@ static inline int find_tpm2_auto_data(
|
||||
int start_token,
|
||||
uint32_t *ret_hash_pcr_mask,
|
||||
uint16_t *ret_pcr_bank,
|
||||
void **ret_pubkey,
|
||||
size_t *ret_pubkey_size,
|
||||
struct iovec *ret_pubkey,
|
||||
uint32_t *ret_pubkey_pcr_mask,
|
||||
uint16_t *ret_primary_alg,
|
||||
void **ret_blob,
|
||||
size_t *ret_blob_size,
|
||||
void **ret_policy_hash,
|
||||
size_t *ret_policy_hash_size,
|
||||
void **ret_salt,
|
||||
size_t *ret_salt_size,
|
||||
void **ret_srk_buf,
|
||||
size_t *ret_srk_size,
|
||||
struct iovec *ret_blob,
|
||||
struct iovec *ret_policy_hash,
|
||||
struct iovec *ret_salt,
|
||||
struct iovec *ret_srk,
|
||||
TPM2Flags *ret_flags,
|
||||
int *ret_keyslot,
|
||||
int *ret_token) {
|
||||
|
||||
+25
-30
@@ -1650,18 +1650,16 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
|
||||
struct crypt_device *cd,
|
||||
const char *name,
|
||||
const char *key_file,
|
||||
const void *key_data,
|
||||
size_t key_data_size,
|
||||
const struct iovec *key_data,
|
||||
usec_t until,
|
||||
uint32_t flags,
|
||||
bool pass_volume_key) {
|
||||
|
||||
_cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
|
||||
_cleanup_(erase_and_freep) void *decrypted_key = NULL;
|
||||
_cleanup_(iovec_done_erase) struct iovec decrypted_key = {};
|
||||
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
|
||||
_cleanup_free_ char *friendly = NULL;
|
||||
int keyslot = arg_key_slot, r;
|
||||
size_t decrypted_key_size;
|
||||
|
||||
assert(cd);
|
||||
assert(name);
|
||||
@@ -1672,7 +1670,7 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
|
||||
return log_oom();
|
||||
|
||||
for (;;) {
|
||||
if (key_file || key_data) {
|
||||
if (key_file || iovec_is_set(key_data)) {
|
||||
/* If key data is specified, use that */
|
||||
|
||||
r = acquire_tpm2_key(
|
||||
@@ -1680,21 +1678,21 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
|
||||
arg_tpm2_device,
|
||||
arg_tpm2_pcr_mask == UINT32_MAX ? TPM2_PCR_MASK_DEFAULT : arg_tpm2_pcr_mask,
|
||||
UINT16_MAX,
|
||||
/* pubkey= */ NULL, /* pubkey_size= */ 0,
|
||||
/* pubkey= */ NULL,
|
||||
/* pubkey_pcr_mask= */ 0,
|
||||
/* signature_path= */ NULL,
|
||||
/* pcrlock_path= */ NULL,
|
||||
/* primary_alg= */ 0,
|
||||
key_file, arg_keyfile_size, arg_keyfile_offset,
|
||||
key_data, key_data_size,
|
||||
/* policy_hash= */ NULL, /* policy_hash_size= */ 0, /* we don't know the policy hash */
|
||||
/* salt= */ NULL, /* salt_size= */ 0,
|
||||
/* srk_buf= */ NULL, /* srk_buf_size= */ 0,
|
||||
key_data,
|
||||
/* policy_hash= */ NULL, /* we don't know the policy hash */
|
||||
/* salt= */ NULL,
|
||||
/* srk= */ NULL,
|
||||
arg_tpm2_pin ? TPM2_FLAGS_USE_PIN : 0,
|
||||
until,
|
||||
arg_headless,
|
||||
arg_ask_password_flags,
|
||||
&decrypted_key, &decrypted_key_size);
|
||||
&decrypted_key);
|
||||
if (r >= 0)
|
||||
break;
|
||||
if (IN_SET(r, -EACCES, -ENOLCK))
|
||||
@@ -1725,8 +1723,7 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
|
||||
}
|
||||
|
||||
if (r == -EOPNOTSUPP) { /* Plugin not available, let's process TPM2 stuff right here instead */
|
||||
_cleanup_free_ void *blob = NULL, *policy_hash = NULL;
|
||||
size_t blob_size, policy_hash_size;
|
||||
_cleanup_(iovec_done) struct iovec blob = {}, policy_hash = {};
|
||||
bool found_some = false;
|
||||
int token = 0; /* first token to look at */
|
||||
|
||||
@@ -1735,8 +1732,7 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
|
||||
* works. */
|
||||
|
||||
for (;;) {
|
||||
_cleanup_free_ void *pubkey = NULL, *salt = NULL, *srk_buf = NULL;
|
||||
size_t pubkey_size = 0, salt_size = 0, srk_buf_size = 0;
|
||||
_cleanup_(iovec_done) struct iovec pubkey = {}, salt = {}, srk = {};
|
||||
uint32_t hash_pcr_mask, pubkey_pcr_mask;
|
||||
uint16_t pcr_bank, primary_alg;
|
||||
TPM2Flags tpm2_flags;
|
||||
@@ -1747,13 +1743,13 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
|
||||
token, /* search for the token with this index, or any later index than this */
|
||||
&hash_pcr_mask,
|
||||
&pcr_bank,
|
||||
&pubkey, &pubkey_size,
|
||||
&pubkey,
|
||||
&pubkey_pcr_mask,
|
||||
&primary_alg,
|
||||
&blob, &blob_size,
|
||||
&policy_hash, &policy_hash_size,
|
||||
&salt, &salt_size,
|
||||
&srk_buf, &srk_buf_size,
|
||||
&blob,
|
||||
&policy_hash,
|
||||
&salt,
|
||||
&srk,
|
||||
&tpm2_flags,
|
||||
&keyslot,
|
||||
&token);
|
||||
@@ -1778,21 +1774,21 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
|
||||
arg_tpm2_device,
|
||||
hash_pcr_mask,
|
||||
pcr_bank,
|
||||
pubkey, pubkey_size,
|
||||
&pubkey,
|
||||
pubkey_pcr_mask,
|
||||
arg_tpm2_signature,
|
||||
arg_tpm2_pcrlock,
|
||||
primary_alg,
|
||||
/* key_file= */ NULL, /* key_file_size= */ 0, /* key_file_offset= */ 0, /* no key file */
|
||||
blob, blob_size,
|
||||
policy_hash, policy_hash_size,
|
||||
salt, salt_size,
|
||||
srk_buf, srk_buf_size,
|
||||
&blob,
|
||||
&policy_hash,
|
||||
&salt,
|
||||
&srk,
|
||||
tpm2_flags,
|
||||
until,
|
||||
arg_headless,
|
||||
arg_ask_password_flags,
|
||||
&decrypted_key, &decrypted_key_size);
|
||||
&decrypted_key);
|
||||
if (IN_SET(r, -EACCES, -ENOLCK))
|
||||
return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
|
||||
if (r != -EPERM)
|
||||
@@ -1837,17 +1833,16 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
|
||||
|
||||
log_debug("Got one or more potentially relevant udev events, rescanning for TPM2...");
|
||||
}
|
||||
assert(decrypted_key);
|
||||
|
||||
if (pass_volume_key)
|
||||
r = measured_crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags);
|
||||
r = measured_crypt_activate_by_volume_key(cd, name, decrypted_key.iov_base, decrypted_key.iov_len, flags);
|
||||
else {
|
||||
_cleanup_(erase_and_freep) char *base64_encoded = NULL;
|
||||
ssize_t base64_encoded_size;
|
||||
|
||||
/* Before using this key as passphrase we base64 encode it, for compat with homed */
|
||||
|
||||
base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
|
||||
base64_encoded_size = base64mem(decrypted_key.iov_base, decrypted_key.iov_len, &base64_encoded);
|
||||
if (base64_encoded_size < 0)
|
||||
return log_oom();
|
||||
|
||||
@@ -2045,7 +2040,7 @@ static int attach_luks_or_plain_or_bitlk(
|
||||
crypt_get_device_name(cd));
|
||||
|
||||
if (arg_tpm2_device || arg_tpm2_device_auto)
|
||||
return attach_luks_or_plain_or_bitlk_by_tpm2(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key);
|
||||
return attach_luks_or_plain_or_bitlk_by_tpm2(cd, name, key_file, &IOVEC_MAKE(key_data, key_data_size), until, flags, pass_volume_key);
|
||||
if (arg_fido2_device || arg_fido2_device_auto)
|
||||
return attach_luks_or_plain_or_bitlk_by_fido2(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key);
|
||||
if (arg_pkcs11_uri || arg_pkcs11_uri_auto)
|
||||
|
||||
Reference in New Issue
Block a user