tss_esapi/attributes/
session.rs1use crate::{tss2_esys::TPMA_SESSION, Error, Result, WrapperErrorKind};
2use bitfield::bitfield;
3use std::convert::TryFrom;
4
5bitfield! {
8 #[derive(Copy, Clone, Eq, PartialEq)]
10 pub struct SessionAttributes(TPMA_SESSION);
11 impl Debug;
12
13 _, set_continue_session: 0;
14 pub continue_session, _: 0;
15 _, set_audit_exclusive: 1;
16 pub audit_exclusive, _: 1;
17 _, set_audit_reset: 2;
18 pub audit_reset, _: 2;
19 reserved, _: 4, 3; _, set_decrypt: 5;
21 pub decrypt, _: 5;
22 _, set_encrypt: 6;
23 pub encrypt, _: 6;
24 _, set_audit: 7;
25 pub audit, _: 7;
26}
27
28impl SessionAttributes {
29 pub const fn builder() -> SessionAttributesBuilder {
31 SessionAttributesBuilder::new()
32 }
33
34 pub fn validate(&self) -> Result<()> {
36 if self.reserved() != 0 {
37 return Err(Error::local_error(WrapperErrorKind::InvalidParam));
38 }
39 Ok(())
40 }
41}
42
43impl TryFrom<TPMA_SESSION> for SessionAttributes {
44 type Error = Error;
45
46 fn try_from(tss_session_attributes: TPMA_SESSION) -> Result<SessionAttributes> {
47 let result = SessionAttributes(tss_session_attributes);
48 result.validate()?;
49 Ok(result)
50 }
51}
52
53impl TryFrom<SessionAttributes> for TPMA_SESSION {
54 type Error = Error;
55
56 fn try_from(session_attributes: SessionAttributes) -> Result<TPMA_SESSION> {
57 session_attributes.validate()?;
58 Ok(session_attributes.0)
59 }
60}
61
62bitfield! {
65 #[derive(Copy, Clone, Eq, PartialEq)]
67 pub struct SessionAttributesMask(TPMA_SESSION);
68 impl Debug;
69
70 _, use_continue_session: 0;
71 _, use_audit_exclusive: 1;
72 _, use_audit_reset: 2;
73 reserved, _: 4, 3; _, use_decrypt: 5;
75 _, use_encrypt: 6;
76 _, use_audit: 7;
77}
78
79impl SessionAttributesMask {
80 pub const fn builder() -> SessionAttributesBuilder {
82 SessionAttributesBuilder::new()
83 }
84
85 pub fn validate(&self) -> Result<()> {
87 if self.reserved() != 0 {
88 return Err(Error::local_error(WrapperErrorKind::InvalidParam));
89 }
90 Ok(())
91 }
92}
93
94impl TryFrom<TPMA_SESSION> for SessionAttributesMask {
95 type Error = Error;
96
97 fn try_from(tpma_session: TPMA_SESSION) -> Result<SessionAttributesMask> {
98 let result = SessionAttributesMask(tpma_session);
99 result.validate()?;
100 Ok(result)
101 }
102}
103
104impl TryFrom<SessionAttributesMask> for TPMA_SESSION {
105 type Error = Error;
106
107 fn try_from(session_attributes_mask: SessionAttributesMask) -> Result<TPMA_SESSION> {
108 session_attributes_mask.validate()?;
109 Ok(session_attributes_mask.0)
110 }
111}
112
113#[derive(Debug, Copy, Clone)]
119pub struct SessionAttributesBuilder {
120 attributes: SessionAttributes,
121 mask: SessionAttributesMask,
122}
123
124impl SessionAttributesBuilder {
125 pub const fn new() -> SessionAttributesBuilder {
126 SessionAttributesBuilder {
127 attributes: SessionAttributes(0),
128 mask: SessionAttributesMask(0),
129 }
130 }
131
132 pub fn with_continue_session(mut self, set: bool) -> Self {
133 self.attributes.set_continue_session(set);
134 self.mask.use_continue_session(true);
135 self
136 }
137
138 pub fn with_audit_exclusive(mut self, set: bool) -> Self {
139 self.attributes.set_audit_exclusive(set);
140 self.mask.use_audit_exclusive(true);
141 self
142 }
143
144 pub fn with_audit_reset(mut self, set: bool) -> Self {
145 self.attributes.set_audit_reset(set);
146 self.mask.use_audit_reset(true);
147 self
148 }
149
150 pub fn with_decrypt(mut self, set: bool) -> Self {
151 self.attributes.set_decrypt(set);
152 self.mask.use_decrypt(true);
153 self
154 }
155
156 pub fn with_encrypt(mut self, set: bool) -> Self {
157 self.attributes.set_encrypt(set);
158 self.mask.use_encrypt(true);
159 self
160 }
161
162 pub fn with_audit(mut self, set: bool) -> Self {
163 self.attributes.set_audit(set);
164 self.mask.use_audit(true);
165 self
166 }
167
168 pub fn build(self) -> (SessionAttributes, SessionAttributesMask) {
169 (self.attributes, self.mask)
170 }
171}
172
173impl Default for SessionAttributesBuilder {
174 fn default() -> Self {
175 Self::new()
176 }
177}