apache / incubator-teaclave-trustzone-sdk

Teaclave TrustZone SDK enables safe, functional, and ergonomic development of trustlets.
https://teaclave.apache.org
Apache License 2.0
216 stars 61 forks source link

Create a persistent object from an initialized transient object #103

Closed syedelec closed 1 year ago

syedelec commented 1 year ago

Hello

I want to create a persistent object from an initialized transient object however it did not seem to work and unfortunately there are no examples, the only examples concern creating a persistent object with initial data.

According to code documentation, this should be possible. Refer to code.

Here is a sample code:

let mut obj_id = [1u8; 1];
let obj_data_flag = DataFlag::ACCESS_READ | DataFlag::ACCESS_WRITE | DataFlag::ACCESS_WRITE_META;

let mut ec_key = TransientObject::allocate(TransientObjectType::EcdsaKeypair, 256).unwrap();
let attr_curve = AttributeValue::from_value(AttributeId::EccCurve, ElementId::EccCurveNistP256 as u32, 0);
ec_key.generate_key(256, &[attr_curve.into()])?;

match PersistentObject::create(
    ObjectStorageConstants::Private,
    &mut obj_id,
    obj_data_flag,
    Some(ec_key), // Do not work. What to put here?
    b"",
) {
    Err(e) => {
        return Err(e);
    }

    Ok(mut object) => {
        trace_println!("success");
    },
}

Thanks for your help.

DemesneGH commented 1 year ago

Hi @syedelec

I want to create a persistent object from an initialized transient object however it did not seem to work

Could you please paste the error message here? thanks!

syedelec commented 1 year ago

I have this error:

error[E0308]: mismatched types
  --> src/ecdsa.rs:32:14
   |
32 |         Some(ec_key),
   |              ^^^^^^ expected struct `ObjectHandle`, found struct `TransientObject`
DemesneGH commented 1 year ago

I have this error:

error[E0308]: mismatched types
  --> src/ecdsa.rs:32:14
   |
32 |         Some(ec_key),
   |              ^^^^^^ expected struct `ObjectHandle`, found struct `TransientObject`

Try Some(ec_key.0)

syedelec commented 1 year ago

I have another error:

error[E0616]: field `0` of struct `TransientObject` is private
  --> src/ecdsa.rs:32:21
   |
32 |         Some(ec_key.0),
   |                     ^ private field
DemesneGH commented 1 year ago

As a workaround, change this line of code: https://github.com/apache/incubator-teaclave-trustzone-sdk/blob/972760f5bd9777104e653fc31a32da096f1955bd/optee-utee/src/object.rs#L485 to

pub struct TransientObject(pub ObjectHandle);

I will consider which is the best way to add this feature later.

syedelec commented 1 year ago

Unfortunately I have another error even with the workaround

error[E0509]: cannot move out of type `TransientObject`, which implements the `Drop` trait
  --> src/ecdsa.rs:32:14
   |
32 |         Some(ec_key.0),
   |              ^^^^^^^^
   |              |
   |              cannot move out of here
   |              move occurs because `ec_key.0` has type `ObjectHandle`, which does not implement the `Copy` trait
DemesneGH commented 1 year ago

Add a new line #[derive(Copy, Clone)] above this line of code:

https://github.com/apache/incubator-teaclave-trustzone-sdk/blob/972760f5bd9777104e653fc31a32da096f1955bd/optee-utee/src/object.rs#L213-L216

/// An opaque handle on an object.
#[derive(Copy, Clone)]
pub struct ObjectHandle {
    raw: *mut raw::TEE_ObjectHandle,
}
syedelec commented 1 year ago

Thanks it's working now.