ivila commented on code in PR #171:
URL: 
https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/171#discussion_r1973523432


##########
examples/secure_db_abstraction-rs/ta/src/secure_db/backend.rs:
##########
@@ -0,0 +1,90 @@
+// 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 anyhow::{bail, Result};
+use optee_utee::{DataFlag, ObjectStorageConstants, PersistentObject};
+
+// Wrapper functions for OP-TEE raw API
+
+pub fn save_in_secure_storage(obj_id: &[u8], data: &[u8]) -> Result<()> {
+    let obj_data_flag = DataFlag::ACCESS_READ
+        | DataFlag::ACCESS_WRITE
+        | DataFlag::ACCESS_WRITE_META
+        | DataFlag::OVERWRITE;
+
+    match PersistentObject::create(
+        ObjectStorageConstants::Private,
+        obj_id,
+        obj_data_flag,
+        None,
+        data,
+    ) {
+        Err(e) => {
+            bail!("[-] {:?}: failed to create object: {:?}", &obj_id, e);
+        }
+        Ok(_) => {
+            return Ok(());
+        }
+    }
+}
+
+pub fn load_from_secure_storage(obj_id: &[u8]) -> Result<Option<Vec<u8>>> {
+    match PersistentObject::open(
+        ObjectStorageConstants::Private,
+        obj_id,
+        DataFlag::ACCESS_READ | DataFlag::SHARE_READ,
+    ) {
+        Err(e) => match e.kind() {
+            optee_utee::ErrorKind::ItemNotFound => {
+                return Ok(None);
+            }
+            _ => {
+                bail!("[-] {:?}: failed to open object: {:?}", &obj_id, e);
+            }
+        },
+
+        Ok(object) => {
+            let obj_info = object.info()?;
+            let mut buf = vec![0u8; obj_info.data_size() as usize];
+
+            let read_bytes = object.read(&mut buf)?;
+            if read_bytes != obj_info.data_size() as u32 {
+                bail!("[-] {:?}: failed to read data", &obj_id);
+            }
+
+            return Ok(Some(buf));
+        }
+    }
+}
+
+pub fn delete_from_secure_storage(obj_id: &[u8]) -> Result<()> {
+    match PersistentObject::open(
+        ObjectStorageConstants::Private,
+        &obj_id,
+        DataFlag::ACCESS_READ | DataFlag::ACCESS_WRITE_META,
+    ) {
+        Err(e) => {
+            bail!("[-] {:?}: failed to open object: {:?}", &obj_id, e);
+        }
+
+        Ok(mut object) => {
+            object.close_and_delete()?;
+            std::mem::forget(object);

Review Comment:
   Why we need to call `std::mem::forget` here?



##########
examples/secure_db_abstraction-rs/ta/src/secure_db/backend.rs:
##########
@@ -0,0 +1,90 @@
+// 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 anyhow::{bail, Result};
+use optee_utee::{DataFlag, ObjectStorageConstants, PersistentObject};
+
+// Wrapper functions for OP-TEE raw API
+
+pub fn save_in_secure_storage(obj_id: &[u8], data: &[u8]) -> Result<()> {
+    let obj_data_flag = DataFlag::ACCESS_READ
+        | DataFlag::ACCESS_WRITE
+        | DataFlag::ACCESS_WRITE_META
+        | DataFlag::OVERWRITE;
+
+    match PersistentObject::create(
+        ObjectStorageConstants::Private,
+        obj_id,
+        obj_data_flag,
+        None,
+        data,
+    ) {
+        Err(e) => {
+            bail!("[-] {:?}: failed to create object: {:?}", &obj_id, e);
+        }
+        Ok(_) => {
+            return Ok(());
+        }
+    }
+}

Review Comment:
   I think we should just use `map_err`
   ```rust
         PersistentObject::create(
             ObjectStorageConstants::Private,
             obj_id,
             obj_data_flag,
             None,
             data,
         ).map_err(|e| bail!("[-] {:?}: failed to create object: {:?}", 
&obj_id, e))
   ```



##########
examples/secure_db_abstraction-rs/ta/src/secure_db/db.rs:
##########
@@ -0,0 +1,114 @@
+// 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::secure_db::{
+    delete_from_secure_storage, load_from_secure_storage, 
save_in_secure_storage,
+};
+use anyhow::{bail, ensure, Result};
+use std::collections::{HashMap, HashSet};
+
+// SecureStorageDb is a key-value storage for TA to easily store and retrieve 
data.
+// First we store the key list in the secure storage, named as db_name.
+// Then we store the each key-value pairs in the secure storage.
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SecureStorageDb {
+    name: String,
+    key_list: HashSet<String>,
+}
+
+impl SecureStorageDb {
+    pub fn open(name: String) -> Result<Self> {
+        match load_from_secure_storage(name.as_bytes())? {
+            Some(data) => {
+                let key_list = bincode::deserialize(&data)?;
+                Ok(Self { name, key_list })
+            }
+            None => {
+                // create new db
+                Ok(Self {
+                    name,
+                    key_list: HashSet::new(),
+                })
+            }
+        }
+    }
+
+    pub fn put(&mut self, key: String, value: Vec<u8>) -> Result<()> {
+        match save_in_secure_storage(key.as_bytes(), &value) {
+            Ok(_) => {
+                self.key_list.insert(key);
+                self.store_key_list()?;
+            }
+            Err(e) => {
+                bail!("[+] SecureStorage::insert(): save error: {}", e);
+            }
+        };
+        Ok(())
+    }
+
+    pub fn get(&self, key: &str) -> Result<Vec<u8>> {
+        ensure!(self.key_list.contains(key), "Key not found in key list");
+        match load_from_secure_storage(key.as_bytes()) {
+            Ok(Some(data)) => Ok(data),
+            Ok(None) => bail!("[+] SecureStorage::get(): object not found in 
db"),
+            Err(e) => {
+                bail!("[+] SecureStorage::get(): load error: {}", e);
+            }
+        }
+    }
+
+    pub fn delete(&mut self, key: &str) -> Result<()> {
+        // ensure key must exist
+        ensure!(self.key_list.contains(key), "Key not found in key list");
+        match delete_from_secure_storage(key.as_bytes()) {
+            Ok(_) => {
+                self.key_list.remove(key);
+                self.store_key_list()?;
+            }
+            Err(e) => {
+                bail!("[+] SecureStorage::delete(): delete error: {}", e);
+            }
+        };
+        Ok(())
+    }
+
+    pub fn clear(&mut self) -> Result<()> {
+        for key in self.key_list.clone() {
+            self.delete(&key)?;
+        }
+        self.key_list.clear();
+        Ok(())
+    }
+
+    pub fn list_entries_with_prefix(&self, prefix: &str) -> 
Result<HashMap<String, Vec<u8>>> {

Review Comment:
   Can we return an Iterator? Just a suggestion.



##########
examples/secure_db_abstraction-rs/ta/src/secure_db/db.rs:
##########
@@ -0,0 +1,114 @@
+// 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::secure_db::{
+    delete_from_secure_storage, load_from_secure_storage, 
save_in_secure_storage,
+};
+use anyhow::{bail, ensure, Result};
+use std::collections::{HashMap, HashSet};
+
+// SecureStorageDb is a key-value storage for TA to easily store and retrieve 
data.
+// First we store the key list in the secure storage, named as db_name.
+// Then we store the each key-value pairs in the secure storage.
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SecureStorageDb {
+    name: String,
+    key_list: HashSet<String>,
+}
+
+impl SecureStorageDb {
+    pub fn open(name: String) -> Result<Self> {
+        match load_from_secure_storage(name.as_bytes())? {
+            Some(data) => {
+                let key_list = bincode::deserialize(&data)?;
+                Ok(Self { name, key_list })
+            }
+            None => {
+                // create new db
+                Ok(Self {
+                    name,
+                    key_list: HashSet::new(),
+                })
+            }
+        }
+    }
+
+    pub fn put(&mut self, key: String, value: Vec<u8>) -> Result<()> {
+        match save_in_secure_storage(key.as_bytes(), &value) {
+            Ok(_) => {
+                self.key_list.insert(key);
+                self.store_key_list()?;
+            }
+            Err(e) => {
+                bail!("[+] SecureStorage::insert(): save error: {}", e);
+            }
+        };
+        Ok(())
+    }
+
+    pub fn get(&self, key: &str) -> Result<Vec<u8>> {
+        ensure!(self.key_list.contains(key), "Key not found in key list");
+        match load_from_secure_storage(key.as_bytes()) {
+            Ok(Some(data)) => Ok(data),
+            Ok(None) => bail!("[+] SecureStorage::get(): object not found in 
db"),
+            Err(e) => {
+                bail!("[+] SecureStorage::get(): load error: {}", e);
+            }
+        }
+    }
+
+    pub fn delete(&mut self, key: &str) -> Result<()> {
+        // ensure key must exist
+        ensure!(self.key_list.contains(key), "Key not found in key list");
+        match delete_from_secure_storage(key.as_bytes()) {
+            Ok(_) => {
+                self.key_list.remove(key);
+                self.store_key_list()?;
+            }
+            Err(e) => {
+                bail!("[+] SecureStorage::delete(): delete error: {}", e);
+            }
+        };
+        Ok(())
+    }
+
+    pub fn clear(&mut self) -> Result<()> {
+        for key in self.key_list.clone() {
+            self.delete(&key)?;
+        }
+        self.key_list.clear();
+        Ok(())

Review Comment:
   Shouldn't the key_list delete the key in the same step as the storage? What 
if the deletion fails midway through processing the key_list?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org
For additional commands, e-mail: dev-h...@teaclave.apache.org

Reply via email to