Skip to content

Commit e0d0092

Browse files
tpm: Provider build: Check Stored Public Information of Root Key
The Public Part of the Root Key in the TPM generated by the tss-esapi layer in the TPM can be checked with a previously public part stored internally. Up until this commit, the storing of that information is not done in the code: This commit only covers the part of checking the Public Part, not of storing it. Signed-off-by: Tomás González <[email protected]>
1 parent d7c37a5 commit e0d0092

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

src/providers/tpm/mod.rs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! for their Parsec operations.
77
use super::Provide;
88
use crate::authenticators::ApplicationIdentity;
9-
use crate::key_info_managers::KeyInfoManagerClient;
9+
use crate::key_info_managers::{KeyIdentity, KeyInfoManagerClient};
1010
use crate::providers::crypto_capability::CanDoCrypto;
1111
use crate::providers::ProviderIdentity;
1212
use derivative::Derivative;
@@ -416,7 +416,8 @@ impl ProviderBuilder {
416416
builder = builder.with_hierarchy_auth(Hierarchy::Endorsement, endorsement_auth);
417417
self.endorsement_hierarchy_auth.zeroize();
418418
}
419-
Ok(Provider::new(
419+
420+
let built_provider = Provider::new(
420421
self.provider_name.ok_or_else(|| {
421422
std::io::Error::new(ErrorKind::InvalidData, "missing provider name")
422423
})?,
@@ -427,6 +428,58 @@ impl ProviderBuilder {
427428
format_error!("Error creating TSS Transient Object Context", e);
428429
std::io::Error::new(ErrorKind::InvalidData, "failed initializing TSS context")
429430
})?,
430-
))
431+
);
432+
433+
// Get the root key from the key store
434+
let root_key_identity = KeyIdentity::new(
435+
ApplicationIdentity::new_internal(),
436+
built_provider.provider_identity.clone(),
437+
String::from("RootKeyTPM"),
438+
);
439+
let key_is_stored = match built_provider
440+
.key_info_store
441+
.does_not_exist(&root_key_identity)
442+
{
443+
Ok(()) => false,
444+
Err(ResponseStatus::PsaErrorAlreadyExists) => true,
445+
Err(e) => Err(e).map_err(|e| {
446+
format_error!("Failure accessing Key Info Manager", e);
447+
std::io::Error::new(ErrorKind::InvalidData, "Key existence check failed")
448+
})?,
449+
};
450+
451+
if key_is_stored {
452+
let stored_root_key_name: Vec<u8> = built_provider
453+
.key_info_store
454+
.get_key_id(&root_key_identity)
455+
.map_err(|e| {
456+
format_error!("Error getting Key Identities from the Key Info Store", e);
457+
std::io::Error::new(ErrorKind::InvalidData, "failed getting Key Identities")
458+
})?;
459+
// Check if the stored public part coincides with the one in the context
460+
let mut esapi_context = built_provider
461+
.esapi_context
462+
.lock()
463+
.expect("ESAPI Context lock poisoned");
464+
465+
let root_key_name = esapi_context.get_root_key_name().map_err(|e| {
466+
format_error!("Error getting the Root Key's name", e);
467+
std::io::Error::new(
468+
ErrorKind::InvalidData,
469+
"failed getting Root Key's Name",
470+
)
471+
})?;
472+
473+
if root_key_name.value().to_vec() != stored_root_key_name {
474+
let e = std::io::Error::new(
475+
ErrorKind::InvalidData,
476+
"Obtained Root Key name does not coincide with the stored one",
477+
);
478+
format_error!("Error when verifying the Root Key's Name", e);
479+
return Err(e);
480+
}
481+
}
482+
483+
Ok(built_provider)
431484
}
432485
}

0 commit comments

Comments
 (0)