Skip to content

Commit 14d1810

Browse files
authored
Merge pull request #587 from muxi/noncetpm
Add tr_sess_get_nonce_tpm interface
2 parents 937062d + 0c69ae2 commit 14d1810

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

tss-esapi/src/context/session_administration.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// SPDX-License-Identifier: Apache-2.0
33
use crate::{
44
attributes::{SessionAttributes, SessionAttributesMask},
5+
ffi::take_from_esys,
56
handles::SessionHandle,
67
interface_types::session_handles::AuthSession,
7-
tss2_esys::{Esys_TRSess_GetAttributes, Esys_TRSess_SetAttributes},
8+
structures::Nonce,
9+
tss2_esys::{Esys_TRSess_GetAttributes, Esys_TRSess_GetNonceTPM, Esys_TRSess_SetAttributes},
810
Context, Result, ReturnCode,
911
};
1012
use log::error;
@@ -51,5 +53,63 @@ impl Context {
5153
Ok(SessionAttributes(flags))
5254
}
5355

54-
// Missing function: Esys_TRSess_GetNonceTPM
56+
/// Get the TPM nonce from a session.
57+
///
58+
/// # Arguments
59+
/// * `session` - An [AuthSession] handle to the authentication session from which to retrieve
60+
/// the TPM nonce.
61+
///
62+
/// # Returns
63+
/// The TPM nonce as a [Nonce] struct on success.
64+
///
65+
/// # Details
66+
/// This function retrieves the nonceTPM value from an authentication session.
67+
///
68+
/// Extracted nonceTPM can be useful in some scenarios. For example, a TPM object protected by a
69+
/// PolicySigned policy requires the nonceTPM value to be extracted and included in the signed
70+
/// digest to satisfy the policy.
71+
///
72+
/// # Example
73+
/// ```rust
74+
/// # use tss_esapi::{Context, TctiNameConf};
75+
/// # use tss_esapi::constants::SessionType;
76+
/// # use tss_esapi::interface_types::algorithm::HashingAlgorithm;
77+
/// # use tss_esapi::structures::SymmetricDefinition;
78+
///
79+
/// let mut context = Context::new(
80+
/// TctiNameConf::from_environment_variable().expect("Failed to get TCTI"),
81+
/// ).expect("Failed to create context");
82+
///
83+
/// let session = context
84+
/// .start_auth_session(
85+
/// None,
86+
/// None,
87+
/// None,
88+
/// SessionType::Policy,
89+
/// SymmetricDefinition::AES_256_CFB,
90+
/// HashingAlgorithm::Sha256,
91+
/// )
92+
/// .expect("Failed to create session")
93+
/// .expect("Received invalid handle");
94+
/// let nonce_tpm = context.tr_sess_get_nonce_tpm(session).expect("Failed to get nonceTPM");
95+
/// // Use the nonce_tpm value as needed
96+
/// ```
97+
pub fn tr_sess_get_nonce_tpm(&mut self, session: AuthSession) -> Result<Nonce> {
98+
let mut nonce_ptr = std::ptr::null_mut();
99+
ReturnCode::ensure_success(
100+
unsafe {
101+
Esys_TRSess_GetNonceTPM(
102+
self.mut_context(),
103+
SessionHandle::from(session).into(),
104+
&mut nonce_ptr,
105+
)
106+
},
107+
|ret| {
108+
error!("Error when getting session nonceTPM: {:#010X}", ret);
109+
},
110+
)?;
111+
112+
let nonce_tpm = unsafe { take_from_esys(nonce_ptr)? };
113+
nonce_tpm.try_into()
114+
}
55115
}

tss-esapi/tests/integration_tests/context_tests/tpm_commands/session_commands_tests.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,30 @@ mod test_start_auth_session {
138138
.unwrap_err();
139139
});
140140
}
141+
142+
#[test]
143+
fn test_get_nonce_tpm() {
144+
let mut context = create_ctx_without_session();
145+
let session = context
146+
.start_auth_session(
147+
None,
148+
None,
149+
None,
150+
SessionType::Policy,
151+
SymmetricDefinition::AES_256_CFB,
152+
HashingAlgorithm::Sha256,
153+
)
154+
.unwrap()
155+
.expect("Received invalid handle");
156+
157+
// Get the TPM nonce from the session
158+
let nonce_tpm = context
159+
.tr_sess_get_nonce_tpm(session)
160+
.expect("Failed to get nonceTPM");
161+
162+
// Verify the nonce is not empty
163+
assert!(!nonce_tpm.as_bytes().is_empty());
164+
}
141165
}
142166

143167
mod test_policy_restart {

0 commit comments

Comments
 (0)