HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/tpm2-util.c
Git blob: 4aac8538339b20bce33c9a3fc2508a4679393682
Lines 12001–12273 of 12,273
r = tpm2_pcr_values_from_string(arg, &pcr_values, &n_pcr_values);
if (r < 0)
return log_error_errno(r, "Could not parse PCR values from '%s': %m", arg);
tpm2_pcr_values_apply_default_hash_alg(pcr_values, n_pcr_values);
tpm2_sort_pcr_values(pcr_values, n_pcr_values);
if (!tpm2_pcr_values_valid(pcr_values, n_pcr_values))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Parsed PCR values are not valid.");
*ret_pcr_values = TAKE_PTR(pcr_values);
*ret_n_pcr_values = n_pcr_values;
return 0;
#else
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support is disabled.");
#endif
}
/* Same as tpm2_parse_pcr_argument(), but the pcr values array is appended to. If the provided pcr values
* array is not NULL, it must point to an allocated pcr values array and the provided number of pcr values
* must be correct.
*
* Note that 'arg' is parsed into a new array of pcr values independently of any previous pcr values,
* including application of the default hash algorithm. Then the two arrays are combined, the default hash
* algorithm check applied again (in case either the previous or current array had no default hash
* algorithm), and then the resulting array is sorted and rechecked for validity. */
int tpm2_parse_pcr_argument_append(const char *arg, Tpm2PCRValue **pcr_values, size_t *n_pcr_values) {
#if HAVE_TPM2
int r;
assert(arg);
assert(pcr_values);
assert(n_pcr_values);
_cleanup_free_ Tpm2PCRValue *more_pcr_values = NULL;
size_t n_more_pcr_values;
r = tpm2_parse_pcr_argument(arg, &more_pcr_values, &n_more_pcr_values);
if (r < 0)
return r;
/* If we got previous values, append them. */
if (*pcr_values && !GREEDY_REALLOC_APPEND(more_pcr_values, n_more_pcr_values, *pcr_values, *n_pcr_values))
return log_oom();
tpm2_pcr_values_apply_default_hash_alg(more_pcr_values, n_more_pcr_values);
tpm2_sort_pcr_values(more_pcr_values, n_more_pcr_values);
if (!tpm2_pcr_values_valid(more_pcr_values, n_more_pcr_values))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Parsed PCR values are not valid.");
SWAP_TWO(*pcr_values, more_pcr_values);
*n_pcr_values = n_more_pcr_values;
return 0;
#else
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support is disabled.");
#endif
}
int tpm2_parse_pcr_argument_to_mask(const char *arg, uint32_t *mask) {
#if HAVE_TPM2
int r;
/* Same as tpm2_parse_pcr_argument() but converts the pcr values to a pcr mask. If a hash algorithm or
* hash value is specified an error is generated (after all we only return the mask here, nothing
* else). This retains the previous behavior of tpm2_parse_pcr_argument() of clearing the mask if
* 'arg' is empty, replacing the mask if it is set to UINT32_MAX, and or-ing the mask otherwise. */
assert(arg);
assert(mask);
_cleanup_free_ Tpm2PCRValue *pcr_values = NULL;
size_t n_pcr_values;
r = tpm2_parse_pcr_argument(arg, &pcr_values, &n_pcr_values);
if (r < 0)
return r;
if (n_pcr_values == 0) {
/* This retains the previous behavior of clearing the mask if the arg is empty */
*mask = 0;
return 0;
}
FOREACH_ARRAY(v, pcr_values, n_pcr_values) {
if (v->hash != 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Not expecting hash algorithm specification in PCR mask value, refusing: %s", arg);
if (v->value.size != 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Not expecting hash value specification in PCR mask value, refusing: %s", arg);
}
uint32_t new_mask;
r = tpm2_pcr_values_to_mask(pcr_values, n_pcr_values, /* hash= */ 0, &new_mask);
if (r < 0)
return log_error_errno(r, "Could not get pcr values mask: %m");
if (*mask == UINT32_MAX)
*mask = new_mask;
else
*mask |= new_mask;
return 0;
#else
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support is disabled.");
#endif
}
int tpm2_load_pcr_signature(const char *path, sd_json_variant **ret) {
_cleanup_strv_free_ char **search = NULL;
_cleanup_free_ char *discovered_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
/* Tries to load a JSON PCR signature file. Takes an absolute path, a simple file name or NULL. In
* the latter two cases searches in /etc/, /usr/lib/, /run/, as usual. */
search = strv_new(CONF_PATHS("systemd"));
if (!search)
return log_oom_debug();
if (!path) {
/* If no path is specified, then look for "tpm2-pcr-signature.json" automatically. Also, in
* this case include /.extra/ in the search path, but only in this case, and if we run in the
* initrd. We don't want to be too eager here, after all /.extra/ is untrusted territory. */
path = "tpm2-pcr-signature.json";
if (in_initrd())
if (strv_extend(&search, "/.extra") < 0)
return log_oom_debug();
}
r = search_and_fopen(path, "re", NULL, (const char**) search, &f, &discovered_path);
if (r < 0)
return log_debug_errno(r, "Failed to find TPM PCR signature file '%s': %m", path);
r = sd_json_parse_file(f, discovered_path, 0, ret, NULL, NULL);
if (r == -ENOMEM)
return log_oom_debug();
if (r < 0) {
log_debug_errno(r, "Failed to parse TPM PCR signature JSON object '%s': %m", discovered_path);
/* Don't propagate the parser's errno: it reports a string that is not valid UTF-8 as
* -EUCLEAN, and to our callers that errno means "a PCR kept being extended". Report a
* malformed file as such instead, whichever way it is malformed. */
return -EBADMSG;
}
return 0;
}
int tpm2_load_pcr_public_key(const char *path, void **ret_pubkey, size_t *ret_pubkey_size) {
_cleanup_strv_free_ char **search = NULL;
_cleanup_free_ char *discovered_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
/* Tries to load a PCR public key file. Takes an absolute path, a simple file name or NULL. In the
* latter two cases searches in /etc/, /usr/lib/, /run/, as usual. */
search = strv_new(CONF_PATHS("systemd"));
if (!search)
return log_oom_debug();
if (!path) {
path = "tpm2-pcr-public-key.pem";
if (in_initrd())
if (strv_extend(&search, "/.extra") < 0)
return log_oom_debug();
}
r = search_and_fopen(path, "re", NULL, (const char**) search, &f, &discovered_path);
if (r < 0)
return log_debug_errno(r, "Failed to find TPM PCR public key file '%s': %m", path);
r = read_full_stream(f, (char**) ret_pubkey, ret_pubkey_size);
if (r < 0)
return log_debug_errno(r, "Failed to load TPM PCR public key PEM file '%s': %m", discovered_path);
return 0;
}
#define PBKDF2_HMAC_SHA256_ITERATIONS 10000
/*
* Implements PBKDF2 HMAC SHA256 for a derived keylen of 32
* bytes and for PBKDF2_HMAC_SHA256_ITERATIONS count.
* I found the wikipedia entry relevant and it contains links to
* relevant RFCs:
* - https://en.wikipedia.org/wiki/PBKDF2
* - https://www.rfc-editor.org/rfc/rfc2898#section-5.2
*/
int tpm2_util_pbkdf2_hmac_sha256(const void *pass,
size_t passlen,
const void *salt,
size_t saltlen,
uint8_t ret[static SHA256_DIGEST_SIZE]) {
_cleanup_(erase_and_freep) uint8_t *buffer = NULL;
uint8_t u[SHA256_DIGEST_SIZE];
/* To keep this simple, since derived KeyLen (dkLen in docs)
* Is the same as the hash output, we don't need multiple
* blocks. Part of the algorithm is to add the block count
* in, but this can be hardcoded to 1.
*/
static const uint8_t block_cnt[] = { 0, 0, 0, 1 };
assert(salt);
assert(saltlen > 0);
assert(saltlen <= (SIZE_MAX - sizeof(block_cnt)));
assert(passlen > 0);
/*
* Build a buffer of salt + block_cnt and hmac_sha256 it we
* do this as we don't have a context builder for HMAC_SHA256.
*/
buffer = malloc(saltlen + sizeof(block_cnt));
if (!buffer)
return -ENOMEM;
memcpy(buffer, salt, saltlen);
memcpy(&buffer[saltlen], block_cnt, sizeof(block_cnt));
hmac_sha256(pass, passlen, buffer, saltlen + sizeof(block_cnt), u);
/* dk needs to be an unmodified u as u gets modified in the loop */
memcpy(ret, u, SHA256_DIGEST_SIZE);
uint8_t *dk = ret;
for (size_t i = 1; i < PBKDF2_HMAC_SHA256_ITERATIONS; i++) {
hmac_sha256(pass, passlen, u, sizeof(u), u);
for (size_t j=0; j < sizeof(u); j++)
dk[j] ^= u[j];
}
return 0;
}
static const char* const tpm2_pcr_index_table[_TPM2_PCR_INDEX_MAX_DEFINED] = {
[TPM2_PCR_PLATFORM_CODE] = "platform-code",
[TPM2_PCR_PLATFORM_CONFIG] = "platform-config",
[TPM2_PCR_EXTERNAL_CODE] = "external-code",
[TPM2_PCR_EXTERNAL_CONFIG] = "external-config",
[TPM2_PCR_BOOT_LOADER_CODE] = "boot-loader-code",
[TPM2_PCR_BOOT_LOADER_CONFIG] = "boot-loader-config",
[TPM2_PCR_HOST_PLATFORM] = "host-platform",
[TPM2_PCR_SECURE_BOOT_POLICY] = "secure-boot-policy",
[TPM2_PCR_KERNEL_INITRD] = "kernel-initrd",
[TPM2_PCR_IMA] = "ima",
[TPM2_PCR_KERNEL_BOOT] = "kernel-boot",
[TPM2_PCR_KERNEL_CONFIG] = "kernel-config",
[TPM2_PCR_SYSEXTS] = "sysexts",
[TPM2_PCR_SHIM_POLICY] = "shim-policy",
[TPM2_PCR_SYSTEM_IDENTITY] = "system-identity",
[TPM2_PCR_DEBUG] = "debug",
[TPM2_PCR_APPLICATION_SUPPORT] = "application-support",
};
DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_FALLBACK(tpm2_pcr_index, int, TPM2_PCRS_MAX - 1);
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(tpm2_pcr_index, int);
bool tpm2_nvpcr_name_is_valid(const char *name) {
return string_is_safe(name, STRING_FILENAME) &&
tpm2_pcr_index_from_string(name) < 0; /* don't allow nvpcrs to be name like pcrs */
}