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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License..

use crate::sys::error::FsResult;
use crate::sys::file::OpenMode;
use crate::sys::host::HostFs;
use crate::sys::keys::{DeriveKey, KeyType, RestoreKey};
use crate::sys::node::{META_DATA_PHY_NUM, NODE_SIZE};
use sgx_crypto::aes::gcm::{Aad, AesGcm, Nonce};
use sgx_types::error::SgxStatus;
use sgx_types::types::{Attributes, CpuSvn, Key128bit, KeyId, KeyPolicy, Mac128bit};
use std::ffi::CStr;
use std::mem;

pub const SGX_FILE_ID: u64 = 0x5347_585F_4649_4C45;
pub const SGX_FILE_MAJOR_VERSION: u8 = 0x01;
pub const SGX_FILE_MINOR_VERSION: u8 = 0x00;

impl_enum! {
    #[repr(u8)]
    #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
    pub enum EncryptFlags {
        AutoKey = 0x00,
        UserKey = 0x01,
        IntegrityOnly = 0x02,
    }
}

impl EncryptFlags {
    #[inline]
    pub fn is_auto_key(&self) -> bool {
        matches!(*self, EncryptFlags::AutoKey)
    }

    #[inline]
    pub fn is_user_key(&self) -> bool {
        matches!(*self, EncryptFlags::UserKey)
    }

    #[inline]
    pub fn is_integrity_only(&self) -> bool {
        matches!(*self, EncryptFlags::IntegrityOnly)
    }
}

impl From<&OpenMode> for EncryptFlags {
    fn from(mode: &OpenMode) -> Self {
        match mode {
            OpenMode::AutoKey(_) | OpenMode::ExportKey | OpenMode::ImportKey(_) => Self::AutoKey,
            OpenMode::UserKey(_) => Self::UserKey,
            OpenMode::IntegrityOnly => Self::IntegrityOnly,
        }
    }
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct MetadataPlain {
    pub file_id: u64,
    pub major_version: u8,
    pub minor_version: u8,
    pub encrypt_flags: EncryptFlags,
    pub update_flag: u8,
    pub key_policy: KeyPolicy,
    pub isv_svn: u16,
    pub key_id: KeyId,
    pub cpu_svn: CpuSvn,
    pub attribute_mask: Attributes,
    pub gmac: Mac128bit,
}

impl MetadataPlain {
    fn new() -> MetadataPlain {
        MetadataPlain {
            file_id: SGX_FILE_ID,
            major_version: SGX_FILE_MAJOR_VERSION,
            minor_version: SGX_FILE_MINOR_VERSION,
            ..Default::default()
        }
    }
}

pub const MD_USER_DATA_SIZE: usize = NODE_SIZE * 3 / 4; // 3072
pub const FILENAME_MAX_LEN: usize = 260;
pub const PATHNAME_MAX_LEN: usize = 512;
pub const FULLNAME_MAX_LEN: usize = PATHNAME_MAX_LEN + FILENAME_MAX_LEN;

#[derive(Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct MetadataEncrypted {
    pub file_name: [u8; FILENAME_MAX_LEN],
    pub size: usize,
    pub mht_key: Key128bit,
    pub mht_gmac: Mac128bit,
    pub data: [u8; MD_USER_DATA_SIZE],
}

const METADATA_PADDING: usize =
    NODE_SIZE - mem::size_of::<MetadataPlain>() - mem::size_of::<MetadataEncrypted>();
const METADATA_ENCRYPTED: usize = mem::size_of::<MetadataEncrypted>();

#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct Metadata {
    pub plaintext: MetadataPlain,
    pub ciphertext: [u8; METADATA_ENCRYPTED],
    pub padding: [u8; METADATA_PADDING],
}

impl Metadata {
    fn new() -> Metadata {
        Metadata {
            plaintext: MetadataPlain::new(),
            ciphertext: [0_u8; METADATA_ENCRYPTED],
            padding: [0_u8; METADATA_PADDING],
        }
    }
}

#[derive(Copy, Clone, Debug, Default)]
#[repr(C)]
pub struct MetadataNode {
    pub node_number: u64,
    pub metadata: Metadata,
}

impl MetadataNode {
    fn new() -> MetadataNode {
        MetadataNode {
            node_number: META_DATA_PHY_NUM,
            metadata: Metadata::new(),
        }
    }
}

impl_struct_default! {
    Metadata;
    MetadataEncrypted;
}

impl_asref_slice! {
    MetadataPlain;
    MetadataEncrypted;
    Metadata;
    MetadataNode;
}

impl_asmut_slice! {
    MetadataPlain;
    MetadataEncrypted;
    Metadata;
    MetadataNode;
}

impl_struct_ContiguousMemory! {
    MetadataPlain;
    MetadataEncrypted;
    Metadata;
    MetadataNode;
}

#[derive(Clone, Debug, Default)]
pub struct MetadataInfo {
    pub node: MetadataNode,
    pub encrypted_plain: MetadataEncrypted,
}

impl MetadataInfo {
    pub fn new() -> MetadataInfo {
        MetadataInfo {
            node: MetadataNode::new(),
            encrypted_plain: MetadataEncrypted::default(),
        }
    }

    #[inline]
    pub fn integrity_only(&self) -> bool {
        self.encrypt_flags().is_integrity_only()
    }

    #[inline]
    pub fn encrypt_flags(&self) -> EncryptFlags {
        self.node.metadata.plaintext.encrypt_flags
    }

    #[inline]
    pub fn set_encrypt_flags(&mut self, encrypt_flags: EncryptFlags) {
        self.node.metadata.plaintext.encrypt_flags = encrypt_flags;
    }

    #[inline]
    pub fn key_policy(&self) -> KeyPolicy {
        self.node.metadata.plaintext.key_policy
    }

    #[inline]
    pub fn set_key_policy(&mut self, key_policy: KeyPolicy) {
        self.node.metadata.plaintext.key_policy = key_policy;
    }

    #[inline]
    pub fn update_flag(&self) -> bool {
        self.node.metadata.plaintext.update_flag != 0
    }

    #[inline]
    pub fn set_update_flag(&mut self, flag: u8) {
        self.node.metadata.plaintext.update_flag = flag;
    }

    #[inline]
    pub fn file_name(&self) -> FsResult<&str> {
        let len = self
            .encrypted_plain
            .file_name
            .iter()
            .enumerate()
            .find(|x| *x.1 == 0)
            .map(|x| x.0 + 1)
            .ok_or(SgxStatus::Unexpected)?;
        let name = CStr::from_bytes_with_nul(&self.encrypted_plain.file_name[0..len])
            .map_err(|_| SgxStatus::Unexpected)?
            .to_str()
            .map_err(|_| SgxStatus::Unexpected)?;
        Ok(name)
    }

    pub fn encrypt(&mut self, key: &Key128bit) -> FsResult {
        let mac = if !self.integrity_only() {
            let mut aes = AesGcm::new(key, Nonce::zeroed(), Aad::empty())?;
            aes.encrypt(
                self.encrypted_plain.as_ref(),
                self.node.metadata.ciphertext.as_mut(),
            )?
        } else {
            let mut aes = AesGcm::new(key, Nonce::zeroed(), Aad::from(&self.encrypted_plain))?;
            let mac = aes.encrypt(&[], &mut [])?;
            self.node
                .metadata
                .ciphertext
                .as_mut()
                .copy_from_slice(self.encrypted_plain.as_ref());
            mac
        };

        self.node.metadata.plaintext.gmac = mac;

        Ok(())
    }

    pub fn decrypt(&mut self, key: &Key128bit) -> FsResult {
        if !self.integrity_only() {
            let mut aes = AesGcm::new(key, Nonce::zeroed(), Aad::empty())?;
            aes.decrypt(
                self.node.metadata.ciphertext.as_ref(),
                self.encrypted_plain.as_mut(),
                &self.node.metadata.plaintext.gmac,
            )?
        } else {
            let mut aes = AesGcm::new(
                key,
                Nonce::zeroed(),
                Aad::from(&self.node.metadata.ciphertext),
            )?;
            aes.decrypt(&[], &mut [], &self.node.metadata.plaintext.gmac)?;
            self.encrypted_plain
                .as_mut()
                .copy_from_slice(self.node.metadata.ciphertext.as_ref());
        };

        Ok(())
    }

    pub fn derive_key(&mut self, derive: &mut dyn DeriveKey) -> FsResult<Key128bit> {
        let (key, key_id) = derive.derive_key(KeyType::Metadata, 0)?;
        match self.encrypt_flags() {
            EncryptFlags::AutoKey => {
                cfg_if! {
                    if #[cfg(feature = "tfs")] {
                        use sgx_tse::EnclaveReport;
                        use sgx_types::types::Report;

                        self.node.metadata.plaintext.key_id = key_id;

                        let report = Report::get_self();
                        self.node.metadata.plaintext.cpu_svn = report.body.cpu_svn;
                        self.node.metadata.plaintext.isv_svn = report.body.isv_svn;
                    } else {
                        use sgx_types::error::errno::ENOTSUP;
                        bail!(eos!(ENOTSUP));
                    }
                }
            }
            EncryptFlags::UserKey => {
                self.node.metadata.plaintext.key_id = key_id;
            }
            EncryptFlags::IntegrityOnly => {}
        }
        Ok(key)
    }

    pub fn restore_key(&self, restore: &dyn RestoreKey) -> FsResult<Key128bit> {
        match self.encrypt_flags() {
            EncryptFlags::AutoKey => {
                cfg_if! {
                    if #[cfg(feature = "tfs")] {
                        restore.restore_key(
                            KeyType::Metadata,
                            self.node.metadata.plaintext.key_id,
                            Some(self.node.metadata.plaintext.key_policy),
                            Some(self.node.metadata.plaintext.cpu_svn),
                            Some(self.node.metadata.plaintext.isv_svn),
                        )
                    } else {
                        use sgx_types::error::errno::ENOTSUP;
                        bail!(eos!(ENOTSUP));
                    }
                }
            }
            EncryptFlags::UserKey | EncryptFlags::IntegrityOnly => restore.restore_key(
                KeyType::Metadata,
                self.node.metadata.plaintext.key_id,
                None,
                None,
                None,
            ),
        }
    }

    #[inline]
    pub fn read_from_disk(&mut self, file: &mut dyn HostFs) -> FsResult {
        file.read(META_DATA_PHY_NUM, &mut self.node.metadata)
    }

    #[inline]
    pub fn write_to_disk(&mut self, file: &mut dyn HostFs) -> FsResult {
        file.write(META_DATA_PHY_NUM, &self.node.metadata)
    }

    #[inline]
    pub fn write_recovery_file(&self, file: &mut dyn HostFs) -> FsResult {
        file.write(META_DATA_PHY_NUM, &self.node)
    }
}

impl Drop for MetadataInfo {
    fn drop(&mut self) {
        self.encrypted_plain.as_mut().fill(0)
    }
}