tss_esapi/attributes/
session.rs

1use crate::tss2_esys::TPMA_SESSION;
2use bitfield::bitfield;
3
4// SESSION ATTRIBUTES
5
6bitfield! {
7    /// Bitfield representing the session attributes.
8    #[derive(Copy, Clone, Eq, PartialEq)]
9    pub struct SessionAttributes(TPMA_SESSION);
10    impl Debug;
11
12    _, set_continue_session: 0;
13    pub continue_session, _: 0;
14    _, set_audit_exclusive: 1;
15    pub audit_exclusive, _: 1;
16    _, set_audit_reset: 2;
17    pub audit_reset, _: 2;
18    // Reserved 3,4 (Shall be clear)
19    _, set_decrypt: 5;
20    pub decrypt, _: 5;
21    _, set_encrypt: 6;
22    pub encrypt, _: 6;
23    _, set_audit: 7;
24    pub audit, _: 7;
25}
26
27impl SessionAttributes {
28    /// Get a builder for the structure
29    pub const fn builder() -> SessionAttributesBuilder {
30        SessionAttributesBuilder::new()
31    }
32}
33
34impl From<TPMA_SESSION> for SessionAttributes {
35    fn from(tss_session_attributes: TPMA_SESSION) -> SessionAttributes {
36        SessionAttributes(tss_session_attributes)
37    }
38}
39
40impl From<SessionAttributes> for TPMA_SESSION {
41    fn from(session_attributes: SessionAttributes) -> TPMA_SESSION {
42        session_attributes.0
43    }
44}
45
46// SESSION ATTRIBUTES MASK
47
48bitfield! {
49    /// Bitfield representing the session attributes mask.
50    #[derive(Copy, Clone, Eq, PartialEq)]
51    pub struct SessionAttributesMask(TPMA_SESSION);
52    impl Debug;
53
54    _, use_continue_session: 0;
55    _, use_audit_exclusive: 1;
56    _, use_audit_reset: 2;
57    // Reserved 3,4 (Shall be clear)
58    _, use_decrypt: 5;
59    _, use_encrypt: 6;
60    _, use_audit: 7;
61}
62
63impl SessionAttributesMask {
64    /// Get a builder for the structure
65    pub const fn builder() -> SessionAttributesBuilder {
66        SessionAttributesBuilder::new()
67    }
68}
69
70impl From<TPMA_SESSION> for SessionAttributesMask {
71    fn from(tss_session_attributes: TPMA_SESSION) -> SessionAttributesMask {
72        SessionAttributesMask(tss_session_attributes)
73    }
74}
75
76impl From<SessionAttributesMask> for TPMA_SESSION {
77    fn from(session_attributes_mask: SessionAttributesMask) -> TPMA_SESSION {
78        session_attributes_mask.0
79    }
80}
81
82// SESSION ATTRIBUTES ITEMS BUILDER
83
84/// A builder that is used to create
85/// SessionAttributes and a corresponding
86/// SessionAttributesMask.
87#[derive(Debug, Copy, Clone)]
88pub struct SessionAttributesBuilder {
89    attributes: SessionAttributes,
90    mask: SessionAttributesMask,
91}
92
93impl SessionAttributesBuilder {
94    pub const fn new() -> SessionAttributesBuilder {
95        SessionAttributesBuilder {
96            attributes: SessionAttributes(0),
97            mask: SessionAttributesMask(0),
98        }
99    }
100
101    pub fn with_continue_session(mut self, set: bool) -> Self {
102        self.attributes.set_continue_session(set);
103        self.mask.use_continue_session(true);
104        self
105    }
106
107    pub fn with_audit_exclusive(mut self, set: bool) -> Self {
108        self.attributes.set_audit_exclusive(set);
109        self.mask.use_audit_exclusive(true);
110        self
111    }
112
113    pub fn with_audit_reset(mut self, set: bool) -> Self {
114        self.attributes.set_audit_reset(set);
115        self.mask.use_audit_reset(true);
116        self
117    }
118
119    pub fn with_decrypt(mut self, set: bool) -> Self {
120        self.attributes.set_decrypt(set);
121        self.mask.use_decrypt(true);
122        self
123    }
124
125    pub fn with_encrypt(mut self, set: bool) -> Self {
126        self.attributes.set_encrypt(set);
127        self.mask.use_encrypt(true);
128        self
129    }
130
131    pub fn with_audit(mut self, set: bool) -> Self {
132        self.attributes.set_audit(set);
133        self.mask.use_audit(true);
134        self
135    }
136
137    pub fn build(self) -> (SessionAttributes, SessionAttributesMask) {
138        (self.attributes, self.mask)
139    }
140}
141
142impl Default for SessionAttributesBuilder {
143    fn default() -> Self {
144        Self::new()
145    }
146}