Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core-primitives/enclave-api/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ extern "C" {
mu_ra_addr_size: u32,
untrusted_worker_addr: *const u8,
untrusted_worker_addr_size: u32,
encoded_base_dir_str: *const u8,
encoded_base_dir_size: u32,
) -> sgx_status_t;

pub fn init_enclave_sidechain_components(
Expand Down
17 changes: 15 additions & 2 deletions core-primitives/enclave-api/src/enclave_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ use sp_core::ed25519;
/// Trait for base/common Enclave API functions
pub trait EnclaveBase: Send + Sync + 'static {
/// Initialize the enclave (needs to be called once at application startup).
fn init(&self, mu_ra_addr: &str, untrusted_worker_addr: &str) -> EnclaveResult<()>;
fn init(
&self,
mu_ra_addr: &str,
untrusted_worker_addr: &str,
base_dir: &str,
) -> EnclaveResult<()>;

/// Initialize the enclave sidechain components.
fn init_enclave_sidechain_components(&self) -> EnclaveResult<()>;
Expand Down Expand Up @@ -67,11 +72,17 @@ pub trait EnclaveBase: Send + Sync + 'static {

/// EnclaveApi implementation for Enclave struct
impl EnclaveBase for Enclave {
fn init(&self, mu_ra_addr: &str, untrusted_worker_addr: &str) -> EnclaveResult<()> {
fn init(
&self,
mu_ra_addr: &str,
untrusted_worker_addr: &str,
base_dir: &str,
) -> EnclaveResult<()> {
let mut retval = sgx_status_t::SGX_SUCCESS;

let encoded_mu_ra_addr = mu_ra_addr.encode();
let encoded_untrusted_worker_addr = untrusted_worker_addr.encode();
let encoded_base_dir = base_dir.encode();

let result = unsafe {
ffi::init(
Expand All @@ -81,6 +92,8 @@ impl EnclaveBase for Enclave {
encoded_mu_ra_addr.len() as u32,
encoded_untrusted_worker_addr.as_ptr(),
encoded_untrusted_worker_addr.len() as u32,
encoded_base_dir.as_ptr(),
encoded_base_dir.len() as u32,
)
};

Expand Down
3 changes: 2 additions & 1 deletion enclave-runtime/Enclave.edl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ enclave {
/* define ECALLs here. */
public sgx_status_t init(
[in, size=mu_ra_addr_size] uint8_t* mu_ra_addr, uint32_t mu_ra_addr_size,
[in, size=untrusted_worker_addr_size] uint8_t* untrusted_worker_addr, uint32_t untrusted_worker_addr_size
[in, size=untrusted_worker_addr_size] uint8_t* untrusted_worker_addr, uint32_t untrusted_worker_addr_size,
[in, size=encoded_base_dir_size] uint8_t* encoded_base_dir_str, uint32_t encoded_base_dir_size
);

public sgx_status_t init_enclave_sidechain_components();
Expand Down
19 changes: 18 additions & 1 deletion enclave-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ pub unsafe extern "C" fn init(
mu_ra_addr_size: u32,
untrusted_worker_addr: *const u8,
untrusted_worker_addr_size: u32,
encoded_base_dir_str: *const u8,
encoded_base_dir_size: u32,
) -> sgx_status_t {
// Initialize the logging environment in the enclave.
env_logger::init();
Expand Down Expand Up @@ -137,7 +139,22 @@ pub unsafe extern "C" fn init(
Err(e) => return e.into(),
};

match initialization::init_enclave(mu_ra_url, untrusted_worker_url, pwd) {
let base_dir = match String::decode(&mut slice::from_raw_parts(
encoded_base_dir_str,
encoded_base_dir_size as usize,
))
.map_err(Error::Codec)
{
Ok(b) => b,
Err(e) => return e.into(),
};

info!("Setting base_dir to {}", base_dir);
BASE_PATH
.set(PathBuf::from(base_dir))
.expect("We only init this once here; qed.");

match initialization::init_enclave(mu_ra_url, untrusted_worker_url, base_dir) {
Err(e) => e.into(),
Ok(()) => sgx_status_t::SGX_SUCCESS,
}
Expand Down
16 changes: 12 additions & 4 deletions service/src/enclave/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

*/

//! keep this api free from chain-specific types!

use crate::config::Config;
use itp_enclave_api::{
enclave_base::EnclaveBase, error::Error as EnclaveApiError, Enclave, EnclaveResult,
Expand All @@ -23,9 +25,11 @@ use itp_settings::files::{ENCLAVE_FILE, ENCLAVE_TOKEN};
use log::*;
use sgx_types::*;
use sgx_urts::SgxEnclave;
/// keep this api free from chain-specific types!
use std::io::{Read, Write};
use std::{fs::File, path::PathBuf};
use std::{
fs::File,
io::{Read, Write},
path::PathBuf,
};

pub fn enclave_init(config: &Config) -> EnclaveResult<Enclave> {
const LEN: usize = 1024;
Expand Down Expand Up @@ -103,7 +107,11 @@ pub fn enclave_init(config: &Config) -> EnclaveResult<Enclave> {

// create an enclave API and initialize it
let enclave_api = Enclave::new(enclave);
enclave_api.init(&config.mu_ra_url_external(), &config.untrusted_worker_url_external())?;
enclave_api.init(
&config.mu_ra_url_external(),
&config.untrusted_worker_url_external(),
&config.base_dir().display().to_string(),
)?;

Ok(enclave_api)
}