1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2021 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
use crate::{
    attributes::{SessionAttributes, SessionAttributesMask},
    handles::SessionHandle,
    interface_types::session_handles::AuthSession,
    tss2_esys::{Esys_TRSess_GetAttributes, Esys_TRSess_SetAttributes, TPMA_SESSION},
    Context, Error, Result,
};
use log::error;

impl Context {
    /// Set the given attributes on a given session.
    pub fn tr_sess_set_attributes(
        &mut self,
        session: AuthSession,
        attributes: SessionAttributes,
        mask: SessionAttributesMask,
    ) -> Result<()> {
        let ret = unsafe {
            Esys_TRSess_SetAttributes(
                self.mut_context(),
                SessionHandle::from(session).into(),
                attributes.into(),
                mask.into(),
            )
        };
        let ret = Error::from_tss_rc(ret);
        if ret.is_success() {
            Ok(())
        } else {
            error!("Error when setting session attributes: {}", ret);
            Err(ret)
        }
    }

    /// Get session attribute flags.
    pub fn tr_sess_get_attributes(&mut self, session: AuthSession) -> Result<SessionAttributes> {
        let mut flags: TPMA_SESSION = 0;
        let ret = unsafe {
            Esys_TRSess_GetAttributes(
                self.mut_context(),
                SessionHandle::from(session).into(),
                &mut flags,
            )
        };
        let ret = Error::from_tss_rc(ret);
        if ret.is_success() {
            Ok(SessionAttributes(flags))
        } else {
            error!("Error when getting session attributes: {}", ret);
            Err(ret)
        }
    }

    // Missing function: Esys_TRSess_GetNonceTPM
}