Skip to content

Commit d1833e1

Browse files
authored
Merge pull request #363 from mdcarr941/main
Conditionally expose `Esys_TR_GetTpmHandle`
2 parents 67036ba + 22fa163 commit d1833e1

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

tss-esapi/build.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ fn main() {
3737
if has_tss_base_rc_values_52_to_53_req.matches(&tss_version) {
3838
println!("cargo:rustc-cfg=has_tss_base_rc_values_52_to_53")
3939
}
40+
41+
#[cfg(feature = "generate-bindings")]
42+
{
43+
let has_esys_tr_get_tpm_handle_req = VersionReq::parse(">=2.4.0").unwrap();
44+
if has_esys_tr_get_tpm_handle_req.matches(&tss_version) {
45+
println!("cargo:rustc-cfg=has_esys_tr_get_tpm_handle")
46+
}
47+
}
4048
}

tss-esapi/src/context/general_esys_tr.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ impl Context {
8585
Ok(())
8686
}
8787

88+
#[cfg(has_esys_tr_get_tpm_handle)]
89+
/// Retrieve the `TpmHandle` stored in the given object.
90+
pub fn tr_get_tpm_handle(&mut self, object_handle: ObjectHandle) -> Result<TpmHandle> {
91+
use crate::{constants::tss::TPM2_RH_UNASSIGNED, tss2_esys::Esys_TR_GetTpmHandle};
92+
let mut tpm_handle = TPM2_RH_UNASSIGNED;
93+
ReturnCode::ensure_success(
94+
unsafe {
95+
Esys_TR_GetTpmHandle(self.mut_context(), object_handle.into(), &mut tpm_handle)
96+
},
97+
|ret| {
98+
error!("Error when getting TPM handle from ESYS handle: {}", ret);
99+
},
100+
)?;
101+
TpmHandle::try_from(tpm_handle)
102+
}
103+
88104
// Missing function: Esys_TR_Serialize
89105
// Missing function: Esys_TR_Deserialize
90106
}

tss-esapi/tests/integration_tests/context_tests/general_esys_tr_tests.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,4 +375,73 @@ mod test_tr_from_tpm_public {
375375
assert_eq!(expected_name, actual_name);
376376
assert_eq!(expected_data, actual_data);
377377
}
378+
379+
#[cfg(has_esys_tr_get_tpm_handle)]
380+
#[test]
381+
fn test_tr_get_tpm_handle() {
382+
use tss_esapi::handles::TpmHandle;
383+
384+
let nv_index_tpm_handle = NvIndexTpmHandle::new(0x01500024).unwrap();
385+
remove_nv_index_handle_from_tpm(nv_index_tpm_handle, Provision::Owner);
386+
387+
let mut context = create_ctx_without_session();
388+
389+
// closure for cleaning up if a call fails.
390+
let cleanup = |context: &mut Context,
391+
e: tss_esapi::Error,
392+
handle: NvIndexHandle,
393+
fn_name: &str|
394+
-> tss_esapi::Error {
395+
// Set password authorization
396+
context.set_sessions((Some(AuthSession::Password), None, None));
397+
context
398+
.nv_undefine_space(Provision::Owner, handle)
399+
.expect("Failed to call nv_undefine_space");
400+
panic!("{} failed: {}", fn_name, e);
401+
};
402+
403+
// Create nv public.
404+
let nv_index_attributes = NvIndexAttributesBuilder::new()
405+
.with_owner_write(true)
406+
.with_owner_read(true)
407+
.build()
408+
.expect("Failed to create owner nv index attributes");
409+
410+
let nv_public = NvPublicBuilder::new()
411+
.with_nv_index(nv_index_tpm_handle)
412+
.with_index_name_algorithm(HashingAlgorithm::Sha256)
413+
.with_index_attributes(nv_index_attributes)
414+
.with_data_area_size(32)
415+
.build()
416+
.unwrap();
417+
///////////////////////////////////////////////////////////////
418+
// Define space
419+
//
420+
// Set password authorization when creating the space.
421+
context.set_sessions((Some(AuthSession::Password), None, None));
422+
let nv_index_handle = context
423+
.nv_define_space(Provision::Owner, None, nv_public)
424+
.expect("Failed to call nv_define_space");
425+
///////////////////////////////////////////////////////////////
426+
// Get the TPM handle from the NV index handle object handle.
427+
//
428+
// Set password authorization
429+
let actual = context
430+
.tr_get_tpm_handle(nv_index_handle.into())
431+
.map_err(|e| cleanup(&mut context, e, nv_index_handle, "tr_get"))
432+
.expect("Failed to get TPM handle");
433+
///////////////////////////////////////////////////////////////
434+
// Remove undefine the space
435+
//
436+
// Set password authorization
437+
context.set_sessions((Some(AuthSession::Password), None, None));
438+
context
439+
.nv_undefine_space(Provision::Owner, nv_index_handle)
440+
.expect("Failed to call nv_undefine_space");
441+
///////////////////////////////////////////////////////////////
442+
// Check that we got the correct handle
443+
//
444+
let expected = TpmHandle::NvIndex(nv_index_tpm_handle);
445+
assert_eq!(expected, actual);
446+
}
378447
}

0 commit comments

Comments
 (0)