|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | use crate::{ |
4 | 4 | attributes::{SessionAttributes, SessionAttributesMask}, |
| 5 | + ffi::take_from_esys, |
5 | 6 | handles::SessionHandle, |
6 | 7 | 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}, |
8 | 10 | Context, Result, ReturnCode, |
9 | 11 | }; |
10 | 12 | use log::error; |
@@ -51,5 +53,46 @@ impl Context { |
51 | 53 | Ok(SessionAttributes(flags)) |
52 | 54 | } |
53 | 55 |
|
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, AuthSession}; |
| 75 | + /// let mut context = Context::new()?; |
| 76 | + /// let session = context.start_auth_session(None, None, None, SessionType::Policy, SymmetricDefinition::AES_256_CFB, HashingAlgorithm::Sha256)?; |
| 77 | + /// let nonce_tpm = context.tr_sess_get_nonce_tpm(session)?; |
| 78 | + /// // Use the nonce_tpm value as needed |
| 79 | + /// ``` |
| 80 | + pub fn tr_sess_get_nonce_tpm(&mut self, session: AuthSession) -> Result<Nonce> { |
| 81 | + let mut nonce_ptr = std::ptr::null_mut(); |
| 82 | + ReturnCode::ensure_success( |
| 83 | + unsafe { |
| 84 | + Esys_TRSess_GetNonceTPM( |
| 85 | + self.mut_context(), |
| 86 | + SessionHandle::from(session).into(), |
| 87 | + &mut nonce_ptr, |
| 88 | + ) |
| 89 | + }, |
| 90 | + |ret| { |
| 91 | + error!("Error when getting session nonceTPM: {:#010X}", ret); |
| 92 | + }, |
| 93 | + )?; |
| 94 | + |
| 95 | + let nonce_tpm = unsafe { take_from_esys(nonce_ptr)? }; |
| 96 | + nonce_tpm.try_into() |
| 97 | + } |
55 | 98 | } |
0 commit comments