Skip to content

Commit 4534984

Browse files
committed
BLD: serializing data into the enclave
1 parent 2f62ac8 commit 4534984

File tree

5 files changed

+82
-8
lines changed

5 files changed

+82
-8
lines changed

enclave/safetrace/app/src/networking/ipc_listener.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::networking::messages::*;
2-
use sgx_types::sgx_enclave_id_t;
2+
use sgx_types::{sgx_enclave_id_t, sgx_status_t};
33
use futures::{Future, Stream};
44
use std::sync::Arc;
55
use tokio_zmq::prelude::*;
66
use tokio_zmq::{Error, Multipart, Rep};
77

8+
89
pub struct IpcListener {
910
_context: Arc<zmq::Context>,
1011
rep_future: Box<dyn Future<Item = Rep, Error = Error>>,
@@ -49,13 +50,17 @@ pub(self) mod handling {
4950
use crate::networking::messages::*;
5051
use crate::keys_u;
5152
use failure::Error;
52-
use sgx_types::sgx_enclave_id_t;
53+
use sgx_types::{sgx_enclave_id_t, sgx_status_t};
5354
use hex::{FromHex, ToHex};
5455
use std::str;
5556
use rmp_serde::Deserializer;
5657
use serde::Deserialize;
5758
use serde_json::Value;
5859

60+
extern {
61+
fn ecall_add_personal_data(eid: sgx_enclave_id_t, ret: *mut sgx_status_t,
62+
some_string: *const u8, len: usize) -> sgx_status_t;
63+
}
5964

6065
type ResponseResult = Result<IpcResponse, Error>;
6166

@@ -86,6 +91,11 @@ pub(self) mod handling {
8691
//#[logfn(DEBUG)]
8792
// pub fn compute_task(db: &mut DB, input: IpcTask, eid: sgx_enclave_id_t) -> ResponseResult {
8893
pub fn add_personal_data( input: IpcInput, eid: sgx_enclave_id_t) -> ResponseResult {
94+
let mut ret = sgx_status_t::SGX_SUCCESS;
95+
let data = serde_json::to_string(&input).unwrap();
96+
97+
unsafe { ecall_add_personal_data(eid, &mut ret as *mut sgx_status_t, data.as_ptr() as * const u8, data.len()) };
98+
8999
let result = IpcResults::AddPersonalData { status: Status::Passed };
90100
Ok(IpcResponse::AddPersonalData { result })
91101
}

enclave/safetrace/enclave/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ default = []
1414
sgx_types = { git = "https://github.com/apache/teaclave-sgx-sdk.git" }
1515
sgx_tstd = { git = "https://github.com/apache/teaclave-sgx-sdk.git", features = ["backtrace"] }
1616
sgx_trts = { git = "https://github.com/apache/teaclave-sgx-sdk.git" }
17+
18+
[dependencies]
19+
serde = { git = "https://github.com/mesalock-linux/serde-sgx", features = ["derive"] }
20+
serde_json = { git = "https://github.com/mesalock-linux/serde-json-sgx" }
21+
1722
[patch.'https://github.com/apache/teaclave-sgx-sdk.git']
1823
sgx_alloc = { path = "../../incubator-teaclave-sgx-sdk/sgx_alloc" }
1924
sgx_build_helper = { path = "../../incubator-teaclave-sgx-sdk/sgx_build_helper" }

enclave/safetrace/enclave/Enclave.edl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ enclave {
2828

2929
public sgx_status_t say_something([in, size=len] const uint8_t* some_string, size_t len);
3030

31+
public sgx_status_t ecall_add_personal_data([in, size=len] const uint8_t* some_string, size_t len);
32+
3133
public sgx_status_t ecall_get_user_key(
3234
[out] uint8_t sig[65],
3335
[in] uint8_t pubkey[64],
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use sgx_types::*;
2+
use serde::{Serialize, Deserialize};
3+
use std::{slice};
4+
use std::string::String;
5+
use std::vec::Vec;
6+
use std::fmt;
7+
8+
// Structs
9+
#[derive(Serialize, Deserialize, Clone, Debug)]
10+
pub struct GeolocationTime {
11+
lat: i32,
12+
lng: i32,
13+
startTS: i32,
14+
endTS: i32
15+
}
16+
17+
#[derive(Serialize, Deserialize, Clone, Debug)]
18+
pub struct Request {
19+
encryptedUserId: String,
20+
encryptedData: String,
21+
userPubKey: String
22+
}
23+
24+
pub fn ecall_add_personal_data_internal(data_json: *const u8, some_len: usize) -> sgx_status_t {
25+
26+
let str_slice = unsafe { slice::from_raw_parts(data_json, some_len) };
27+
28+
// Input sanitised in EngimaJS to object type
29+
let mut request: Request = serde_json::from_slice(str_slice).unwrap();
30+
31+
// Read from the state
32+
// let mut data = Self::get_data();
33+
// Append
34+
// data.append(&mut array);
35+
// Write back to the state
36+
// write_state!(DATASET => data);
37+
38+
// Ocall to normal world for output
39+
println!("Received Data: {:?}", &request);
40+
41+
sgx_status_t::SGX_SUCCESS
42+
}

enclave/safetrace/enclave/src/lib.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,17 @@ use sgx_types::*;
3030
use std::string::String;
3131
use std::vec::Vec;
3232
use std::io::{self, Write};
33-
use std::slice;
33+
use std::{slice};
3434

35+
extern crate serde;
36+
extern crate serde_json;
37+
38+
39+
mod data;
40+
mod keys_t;
41+
42+
use keys_t::ecall_get_user_key_internal;
43+
use data::ecall_add_personal_data_internal;
3544

3645
#[no_mangle]
3746
pub extern "C" fn say_something(some_string: *const u8, some_len: usize) -> sgx_status_t {
@@ -66,16 +75,22 @@ pub extern "C" fn say_something(some_string: *const u8, some_len: usize) -> sgx_
6675

6776

6877
#[no_mangle]
69-
pub extern "C" fn ecall_get_user_key(sig: &mut [u8; 65], user_pubkey: &[u8; 64], serialized_ptr: *mut u64) -> sgx_status_t {
78+
pub unsafe extern "C" fn ecall_get_user_key(sig: &mut [u8; 65], user_pubkey: &[u8; 64], serialized_ptr: *mut u64) -> sgx_status_t {
7079
println!("Get User Key called inside envlave");
71-
// let msg = match ecall_get_user_key_internal(sig, user_pubkey) {
72-
// Ok(msg) => msg,
73-
// Err(e) => return e.into(),
74-
// };
80+
let msg = match ecall_get_user_key_internal(sig, user_pubkey) {
81+
Ok(msg) => msg,
82+
Err(e) => return e,
83+
};
7584
// *serialized_ptr = match ocalls_t::save_to_untrusted_memory(&msg[..]) {
7685
// Ok(ptr) => ptr,
7786
// Err(e) => return e.into(),
7887
// };
7988
// EnclaveReturn::Success
8089
sgx_status_t::SGX_SUCCESS
90+
}
91+
92+
#[no_mangle]
93+
pub extern "C" fn ecall_add_personal_data(data_string: *const u8, data_len: usize) -> sgx_status_t {
94+
ecall_add_personal_data_internal(data_string, data_len);
95+
sgx_status_t::SGX_SUCCESS
8196
}

0 commit comments

Comments
 (0)