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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use crate::{tss2_esys::TPMA_OBJECT, Result};
use bitfield::bitfield;

bitfield! {
    /// Bitfield representing the object attributes.
    #[derive(Copy, Clone, Eq, PartialEq)]
    pub struct ObjectAttributes(TPMA_OBJECT);
    impl Debug;
    // Object attribute flags
    pub fixed_tpm, _: 1;
    _, set_fixed_tpm: 1;
    pub st_clear, _: 2;
    _, set_st_clear: 2;
    pub fixed_parent, _: 4;
    _, set_fixed_parent: 4;
    pub sensitive_data_origin, _: 5;
    _, set_sensitive_data_origin: 5;
    pub user_with_auth, _: 6;
    _, set_user_with_auth: 6;
    pub admin_with_policy, _: 7;
    _, set_admin_with_policy: 7;
    pub no_da, _: 10;
    _, set_no_da: 10;
    pub encrypted_duplication, _: 11;
    _, set_encrypted_duplication: 11;
    pub restricted, _: 16;
    _, set_restricted: 16;
    pub decrypt, _: 17;
    _, set_decrypt: 17;
    pub sign_encrypt, _: 18;
    _, set_sign_encrypt: 18;
    pub x509_sign, _: 19;
    _, set_x509_sign: 19;
}

impl ObjectAttributes {
    /// Function for creating attributes for a
    /// fixed parent key object.
    pub fn new_fixed_parent_key() -> Self {
        let mut attrs = ObjectAttributes(0);
        attrs.set_fixed_tpm(true);
        attrs.set_fixed_parent(true);
        attrs.set_sensitive_data_origin(true);
        attrs.set_user_with_auth(true);
        attrs.set_decrypt(true);
        attrs.set_restricted(true);
        attrs
    }

    /// Function for creating attributes for
    /// a fixed signing key object.
    pub fn new_fixed_signing_key() -> Self {
        let mut attrs = ObjectAttributes(0);
        attrs.set_fixed_tpm(true);
        attrs.set_fixed_parent(true);
        attrs.set_sensitive_data_origin(true);
        attrs.set_user_with_auth(true);
        attrs.set_sign_encrypt(true);
        attrs
    }

    /// Get a builder for the structure
    pub const fn builder() -> ObjectAttributesBuilder {
        ObjectAttributesBuilder::new()
    }
}

impl From<ObjectAttributes> for TPMA_OBJECT {
    fn from(object_attributes: ObjectAttributes) -> Self {
        object_attributes.0
    }
}

impl From<TPMA_OBJECT> for ObjectAttributes {
    fn from(tpma_object: TPMA_OBJECT) -> Self {
        ObjectAttributes(tpma_object)
    }
}

/// A builder for [ObjectAttributes]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct ObjectAttributesBuilder {
    object_attributes: ObjectAttributes,
}

impl ObjectAttributesBuilder {
    /// Creates an new [ObjectAttributes] builder.
    pub const fn new() -> Self {
        ObjectAttributesBuilder {
            object_attributes: ObjectAttributes(0),
        }
    }

    /// Controls the `fixed tpm` attribute
    ///
    /// # Arguments
    /// * `set` - `true` indicates that the attribute should have the value SET.
    ///           `false`indicates that the attribute should have the value CLEAR.
    pub fn with_fixed_tpm(mut self, set: bool) -> Self {
        self.object_attributes.set_fixed_tpm(set);
        self
    }

    /// Controls the `st clear` attribute
    ///
    /// # Arguments
    /// * `set` - `true` indicates that the attribute should have the value SET.
    ///           `false`indicates that the attribute should have the value CLEAR.
    pub fn with_st_clear(mut self, set: bool) -> Self {
        self.object_attributes.set_st_clear(set);
        self
    }

    /// Controls the `fixed parent` attribute
    ///
    /// # Arguments
    /// * `set` - `true` indicates that the attribute should have the value SET.
    ///           `false`indicates that the attribute should have the value CLEAR.
    pub fn with_fixed_parent(mut self, set: bool) -> Self {
        self.object_attributes.set_fixed_parent(set);
        self
    }

    /// Controls the `sensitive data origin` attribute
    ///
    /// # Arguments
    /// * `set` - `true` indicates that the attribute should have the value SET.
    ///           `false`indicates that the attribute should have the value CLEAR.
    pub fn with_sensitive_data_origin(mut self, set: bool) -> Self {
        self.object_attributes.set_sensitive_data_origin(set);
        self
    }

    /// Controls the `user with auth` attribute
    ///
    /// # Arguments
    /// * `set` - `true` indicates that the attribute should have the value SET.
    ///           `false`indicates that the attribute should have the value CLEAR.
    pub fn with_user_with_auth(mut self, set: bool) -> Self {
        self.object_attributes.set_user_with_auth(set);
        self
    }

    /// Controls the `admin with policy` attribute
    ///
    /// # Arguments
    /// * `set` - `true` indicates that the attribute should have the value SET.
    ///           `false`indicates that the attribute should have the value CLEAR.
    pub fn with_admin_with_policy(mut self, set: bool) -> Self {
        self.object_attributes.set_admin_with_policy(set);
        self
    }

    /// Controls the `no da` attribute
    ///
    /// # Arguments
    /// * `set` - `true` indicates that the attribute should have the value SET.
    ///           `false`indicates that the attribute should have the value CLEAR.
    pub fn with_no_da(mut self, set: bool) -> Self {
        self.object_attributes.set_no_da(set);
        self
    }

    /// Controls the `encrypted duplication` attribute
    ///
    /// # Arguments
    /// * `set` - `true` indicates that the attribute should have the value SET.
    ///           `false`indicates that the attribute should have the value CLEAR.
    pub fn with_encrypted_duplication(mut self, set: bool) -> Self {
        self.object_attributes.set_encrypted_duplication(set);
        self
    }

    /// Controls the `restricted` attribute
    ///
    /// # Arguments
    /// * `set` - `true` indicates that the attribute should have the value SET.
    ///           `false`indicates that the attribute should have the value CLEAR.
    pub fn with_restricted(mut self, set: bool) -> Self {
        self.object_attributes.set_restricted(set);
        self
    }

    /// Controls the `decrypt` attribute
    ///
    /// # Arguments
    /// * `set` - `true` indicates that the attribute should have the value SET.
    ///           `false`indicates that the attribute should have the value CLEAR.
    pub fn with_decrypt(mut self, set: bool) -> Self {
        self.object_attributes.set_decrypt(set);
        self
    }

    /// Controls the `sign/encrypt` attribute
    ///
    /// # Arguments
    /// * `set` - `true` indicates that the attribute should have the value SET.
    ///           `false`indicates that the attribute should have the value CLEAR.
    pub fn with_sign_encrypt(mut self, set: bool) -> Self {
        self.object_attributes.set_sign_encrypt(set);
        self
    }

    /// Controls the `X509 sign` attribute
    ///
    /// # Arguments
    /// * `set` - `true` indicates that the attribute should have the value SET.
    ///           `false`indicates that the attribute should have the value CLEAR.
    pub fn with_x509_sign(mut self, set: bool) -> Self {
        self.object_attributes.set_x509_sign(set);
        self
    }

    /// Builds the nv index attributes.
    ///
    /// # Errors
    /// Returns an error if some attributes are missing
    /// or are in conflict with each other.
    pub fn build(self) -> Result<ObjectAttributes> {
        Ok(self.object_attributes)
    }
}

impl Default for ObjectAttributesBuilder {
    fn default() -> Self {
        ObjectAttributesBuilder::new()
    }
}