Skip to content
1 change: 1 addition & 0 deletions src-tauri/daemon/src/device_identity/tpm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ fn decode_key(key: ipc::TpmKeyBlobs) -> Result<backend::TpmKeyBlobs, Status> {

/// Checks that the caller supplied the fields needed to reload the keys.
/// The backend interprets the TPM encodings and reloads the objects.
/// The EK certificate chain can be empty because it is not needed to reload the keys.
fn decode_identity(identity: ipc::DeviceIdentityMaterial) -> Result<backend::Identity, Status> {
let Some(ipc::device_identity_material::Backend::Tpm(identity)) = identity.backend else {
return Err(Status::invalid_argument("Missing TPM identity material"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub(in super::super) fn attest(
.map_err(|_| DeviceIdentityError::InvalidChallenge)?;
let ek_template = ek::decode_stored_template(&identity.ek.template)?;
let mut transport = tbs::TbsTransport::open()?;
raw_capabilities::read_properties(&mut transport)?.check_hierarchy_auth(false)?;
// Reusing a persistent SRK does not authorize recreation of the EK.
raw_capabilities::read_properties(&mut transport)?.require_empty_endorsement_auth()?;

let (activation_secret, evidence) = keys::with_key_under_srk(
&mut transport,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use tpm2_protocol::{
},
};

use tracing::{debug, info, warn};

use super::{
map_identity_rc, map_tpm_rc, marshal, password_auth, raw_capabilities, receive,
send_authenticated, send_with_sessions_and_tpm_error, with_primary, with_transient_response,
Expand Down Expand Up @@ -94,7 +96,7 @@ pub(super) fn signing_template(key: SigningKey) -> TpmtPublic {
}
}

// The TPM persistent slot reserved for the RSA storage root key.
// Windows conventionally provisions the RSA storage root key in this slot.
const WINDOWS_SRK: u32 = 0x8100_0001;

pub(super) struct CreatedKeys {
Expand All @@ -117,6 +119,9 @@ fn windows_srk(
other => map_tpm_rc(other),
})?;
let public = response.out_public.inner;
// Restricted decryption identifies a storage parent. The fixed attributes keep it
// in its original hierarchy and under its original parent. This path supports the
// conventional Windows RSA SRK and verifies its Name using SHA-256 below.
let required = TpmaObject::FIXED_TPM
| TpmaObject::FIXED_PARENT
| TpmaObject::RESTRICTED
Expand All @@ -127,7 +132,7 @@ fn windows_srk(
{
return Err(DeviceIdentityError::Unsupported);
}
// The Name binds the saved parent to its public area, not just its slot.
// Verify the TPM Name against the public area before using it as the saved parent identifier.
let name = sha256_name(&public)?;
if response.name.as_ref() != name {
return Err(DeviceIdentityError::BackendFailure);
Expand All @@ -151,17 +156,40 @@ pub(super) fn create(
Ok((ak, identity_key))
};
let (storage_parent, (ak, identity_key)) = if use_windows_srk {
debug!(
handle = WINDOWS_SRK,
"Creating TPM identity keys under the Windows SRK"
);
// Reuse the provisioned parent without creating, evicting or flushing it.
let (parent, name) = windows_srk(transport).map_err(|error| match error {
DeviceIdentityError::IdentityLost => DeviceIdentityError::Unsupported,
other => other,
let (parent, name) = windows_srk(transport)
.inspect_err(|error| {
warn!(
handle = WINDOWS_SRK,
?error,
"Cannot use the Windows SRK for TPM identity creation"
);
})
.map_err(|error| match error {
DeviceIdentityError::IdentityLost => DeviceIdentityError::Unsupported,
other => other,
})?;
let children = create_children(transport, parent).inspect_err(|error| {
warn!(
handle = WINDOWS_SRK,
?error,
"Failed to create TPM identity keys under the Windows SRK"
);
})?;
info!(
handle = WINDOWS_SRK,
"Created TPM identity keys under the Windows SRK"
);
(
StorageParent::Persistent {
handle: u32::from(parent),
name,
},
create_children(transport, parent)?,
children,
)
} else {
let template = storage_template();
Expand Down Expand Up @@ -207,19 +235,6 @@ fn create_signing_key(
})
}

#[cfg(test)]
pub(super) fn reload(
transport: &mut impl Transport,
identity: &crate::TpmIdentity,
) -> Result<(), DeviceIdentityError> {
with_key_under_srk(
transport,
&identity.storage_parent,
&identity.ak,
|transport, srk, _| with_loaded_key(transport, srk, &identity.identity_key, |_, _| Ok(())),
)
}

/// The operation receives the SRK handle, then the loaded key handle.
pub(super) fn with_key_under_srk<T: Transport, R>(
transport: &mut T,
Expand All @@ -233,7 +248,7 @@ pub(super) fn with_key_under_srk<T: Transport, R>(
handle,
name: expected_name,
} => {
// Windows only provisions this path against its conventional RSA SRK slot.
// Only the conventional Windows RSA SRK slot is supported by this backend.
if *handle != WINDOWS_SRK {
return Err(DeviceIdentityError::IdentityLost);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,52 @@ fn test_persistent_srk_is_checked_before_loading_child_keys() {
.unwrap();
assert!(transport.0.is_empty());
}

#[test]
fn test_identity_creation_does_not_fall_back_to_owner_when_windows_srk_is_missing() {
use tpm2_protocol::{
data::{TpmCc, TpmSt},
frame::TpmCommand,
};

use super::super::test_response_frame;

struct MissingParent(bool);

impl Transport for MissingParent {
fn submit(&mut self, command: &[u8]) -> Result<Vec<u8>, DeviceIdentityError> {
assert!(!self.0, "unexpected TPM command after ReadPublic failed");
self.0 = true;
assert_eq!(
TpmCommand::cast(command).unwrap().cc().unwrap(),
TpmCc::ReadPublic
);
assert_eq!(&command[10..14], &WINDOWS_SRK.to_be_bytes());
// TPM_RC_HANDLE for the first command handle.
Ok(test_response_frame(TpmSt::NoSessions, 0x18b, &[]))
}
}

let mut transport = MissingParent(false);
assert!(matches!(
create(&mut transport, true),
Err(DeviceIdentityError::Unsupported)
));
assert!(transport.0);
}

#[test]
#[ignore = "Requires a Windows TPM"]
fn test_created_keys_reload_in_fresh_tbs_connection() {
use super::super::{create_identity, tbs::TbsTransport};

let identity = create_identity().expect("create TPM identity");
let mut transport = TbsTransport::open().expect("open a fresh TBS context");
with_key_under_srk(
&mut transport,
&identity.storage_parent,
&identity.ak,
|transport, srk, _| with_loaded_key(transport, srk, &identity.identity_key, |_, _| Ok(())),
)
.expect("reload both keys under the saved SRK");
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ pub(super) fn read_capabilities() -> Result<Capabilities, DeviceIdentityError> {
pub(super) fn create_identity() -> Result<TpmIdentity, DeviceIdentityError> {
let mut transport = tbs::TbsTransport::open()?;
let properties = raw_capabilities::read_properties(&mut transport)?;
properties.check_hierarchy_auth(false)?;
// The EK still needs empty Endorsement authorization even when Windows supplies the SRK.
properties.require_empty_endorsement_auth()?;
let ek = ek::read(&mut transport, properties.nv_buffer_max())?;
let keys::CreatedKeys {
storage_parent,
Expand Down Expand Up @@ -429,14 +430,6 @@ mod tests {

use super::*;

#[test]
#[ignore = "Requires a Windows TPM"]
fn test_created_keys_reload_in_fresh_tbs_connection() {
let identity = create_identity().expect("create TPM identity");
let mut transport = tbs::TbsTransport::open().expect("open a fresh TBS context");
keys::reload(&mut transport, &identity).expect("reload both keys under the recreated SRK");
}

struct FakeTransport {
command: Vec<u8>,
response: Vec<u8>,
Expand Down
Loading
Loading