use crate::{
Context, Result, ReturnCode,
attributes::{SessionAttributes, SessionAttributesMask},
ffi::take_from_esys,
handles::SessionHandle,
interface_types::session_handles::AuthSession,
structures::Nonce,
tss2_esys::{Esys_TRSess_GetAttributes, Esys_TRSess_GetNonceTPM, Esys_TRSess_SetAttributes},
};
use log::error;
use std::convert::TryInto;
impl Context {
pub fn tr_sess_set_attributes(
&mut self,
session: AuthSession,
attributes: SessionAttributes,
mask: SessionAttributesMask,
) -> Result<()> {
ReturnCode::ensure_success(
unsafe {
Esys_TRSess_SetAttributes(
self.mut_context(),
SessionHandle::from(session).into(),
attributes.try_into()?,
mask.try_into()?,
)
},
|ret| {
error!("Error when setting session attributes: {:#010X}", ret);
},
)
}
pub fn tr_sess_get_attributes(&mut self, session: AuthSession) -> Result<SessionAttributes> {
let mut flags = 0;
ReturnCode::ensure_success(
unsafe {
Esys_TRSess_GetAttributes(
self.mut_context(),
SessionHandle::from(session).into(),
&mut flags,
)
},
|ret| {
error!("Error when getting session attributes: {:#010X}", ret);
},
)?;
Ok(SessionAttributes(flags))
}
pub fn tr_sess_get_nonce_tpm(&mut self, session: AuthSession) -> Result<Nonce> {
let mut nonce_ptr = std::ptr::null_mut();
ReturnCode::ensure_success(
unsafe {
Esys_TRSess_GetNonceTPM(
self.mut_context(),
SessionHandle::from(session).into(),
&mut nonce_ptr,
)
},
|ret| {
error!("Error when getting session nonceTPM: {:#010X}", ret);
},
)?;
let nonce_tpm = unsafe { take_from_esys(nonce_ptr)? };
nonce_tpm.try_into()
}
}