r = tpm2_nvpcr_read(*c, /* session= */ NULL, name, &digest, &nv_index);
if (r < 0)
return log_error_errno(r, "Failed to read NvPCR '%s': %m", name);
-
- h = hexmem(digest.iov_base, digest.iov_len);
- if (!h)
- return log_oom();
+ if (r > 0) { /* set? */
+ h = hexmem(digest.iov_base, digest.iov_len);
+ if (!h)
+ return log_oom();
+ }
} else {
r = tpm2_nvpcr_get_index(name, &nv_index);
if (r < 0)
if (r < 0)
return r;
+ /* Check if the NvPCR is already anchored */
+ const char *anchor_fname = strjoina("/run/systemd/nvpcr/", name, ".anchor");
+ r = access_nofollow(anchor_fname, F_OK);
+ if (r < 0) {
+ if (r != -ENOENT)
+ return log_debug_errno(r, "Failed to check if '%s' exists: %m", anchor_fname);
+
+ /* valid, but not anchored */
+ *ret_value = (struct iovec) {};
+ if (ret_nv_index)
+ *ret_nv_index = p.nv_index;
+
+ return 0;
+ }
+
_cleanup_(tpm2_handle_freep) Tpm2Handle *nv_handle = NULL;
r = tpm2_index_to_handle(
c,
log_debug("Successfully acquired handle to NV index 0x%" PRIx32 ".", p.nv_index);
- r = tpm2_read_nv_index(
- c,
- /* session= */ NULL,
- p.nv_index,
- nv_handle,
- ret_value);
- if (r < 0)
- return r;
+ if (r > 0) {
+ r = tpm2_read_nv_index(
+ c,
+ /* session= */ NULL,
+ p.nv_index,
+ nv_handle,
+ ret_value);
+ if (r < 0)
+ return r;
+
+ r = 1;
+ } else {
+ *ret_value = (struct iovec) {};
+ r = 0;
+ }
if (ret_nv_index)
*ret_nv_index = p.nv_index;
- return 0;
+ return r;
#else /* HAVE_OPENSSL */
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "OpenSSL support is disabled.");
#endif