Skip to content

Commit

Permalink
Return encoded extrinsics without padding (#1505)
Browse files Browse the repository at this point in the history
* Trim padded xt

* fix compile
  • Loading branch information
Kailai-Wang authored Dec 4, 2023
1 parent a36ae4b commit e40355f
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 84 deletions.
21 changes: 14 additions & 7 deletions core-primitives/enclave-api/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ extern "C" {
quote: *const u8,
quote_size: u32,
unchecked_extrinsic: *mut u8,
unchecked_extrinsic_size: u32,
unchecked_extrinsic_max_size: u32,
unchecked_extrinsic_size: *mut u32,
) -> sgx_status_t;

pub fn init(
Expand Down Expand Up @@ -135,7 +136,8 @@ extern "C" {
w_url: *const u8,
w_url_size: u32,
unchecked_extrinsic: *mut u8,
unchecked_extrinsic_size: u32,
unchecked_extrinsic_max_size: u32,
unchecked_extrinsic_size: *mut u32,
skip_ra: c_int,
) -> sgx_status_t;

Expand All @@ -145,7 +147,8 @@ extern "C" {
w_url: *const u8,
w_url_size: u32,
unchecked_extrinsic: *mut u8,
unchecked_extrinsic_size: u32,
unchecked_extrinsic_max_size: u32,
unchecked_extrinsic_size: *mut u32,
skip_ra: c_int,
quoting_enclave_target_info: Option<&sgx_target_info_t>,
quote_size: Option<&u32>,
Expand All @@ -166,15 +169,17 @@ extern "C" {
retval: *mut sgx_status_t,
collateral: *const sgx_ql_qve_collateral_t,
unchecked_extrinsic: *mut u8,
unchecked_extrinsic_size: u32,
unchecked_extrinsic_max_size: u32,
unchecked_extrinsic_size: *mut u32,
) -> sgx_status_t;

pub fn generate_register_tcb_info_extrinsic(
eid: sgx_enclave_id_t,
retval: *mut sgx_status_t,
collateral: *const sgx_ql_qve_collateral_t,
unchecked_extrinsic: *mut u8,
unchecked_extrinsic_size: u32,
unchecked_extrinsic_max_size: u32,
unchecked_extrinsic_size: *mut u32,
) -> sgx_status_t;

pub fn dump_ias_ra_cert_to_disk(
Expand Down Expand Up @@ -214,7 +219,8 @@ extern "C" {
fiat_currency: *const u8,
fiat_currency_size: u32,
unchecked_extrinsic: *mut u8,
unchecked_extrinsic_size: u32,
unchecked_extrinsic_max_size: u32,
unchecked_extrinsic_size: *mut u32,
) -> sgx_status_t;

pub fn update_weather_data_xt(
Expand All @@ -225,7 +231,8 @@ extern "C" {
weather_info_latitude: *const u8,
weather_info_latitude_size: u32,
unchecked_extrinsic: *mut u8,
unchecked_extrinsic_size: u32,
unchecked_extrinsic_max_size: u32,
unchecked_extrinsic_size: *mut u32,
) -> sgx_status_t;

pub fn run_state_provisioning_server(
Expand Down
23 changes: 16 additions & 7 deletions core-primitives/enclave-api/src/remote_attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ mod impl_ffi {
let mut retval = sgx_status_t::SGX_SUCCESS;

let mut unchecked_extrinsic: Vec<u8> = vec![0u8; EXTRINSIC_MAX_SIZE];
let mut unchecked_extrinsic_size: u32 = 0;

trace!("Generating dcap_ra_extrinsic with URL: {}", w_url);
trace!("Generating ias_ra_extrinsic with URL: {}", w_url);

let url = w_url.encode();

Expand All @@ -157,14 +158,15 @@ mod impl_ffi {
url.len() as u32,
unchecked_extrinsic.as_mut_ptr(),
unchecked_extrinsic.len() as u32,
&mut unchecked_extrinsic_size as *mut u32,
skip_ra.into(),
)
};

ensure!(result == sgx_status_t::SGX_SUCCESS, Error::Sgx(result));
ensure!(retval == sgx_status_t::SGX_SUCCESS, Error::Sgx(retval));

Ok(unchecked_extrinsic)
Ok(Vec::from(&unchecked_extrinsic[..unchecked_extrinsic_size as usize]))
}
fn generate_dcap_ra_extrinsic_from_quote(
&self,
Expand All @@ -173,6 +175,7 @@ mod impl_ffi {
) -> EnclaveResult<Vec<u8>> {
let mut retval = sgx_status_t::SGX_SUCCESS;
let mut unchecked_extrinsic: Vec<u8> = vec![0u8; EXTRINSIC_MAX_SIZE];
let mut unchecked_extrinsic_size: u32 = 0;
let url = url.encode();

let result = unsafe {
Expand All @@ -185,13 +188,14 @@ mod impl_ffi {
quote.len() as u32,
unchecked_extrinsic.as_mut_ptr(),
unchecked_extrinsic.len() as u32,
&mut unchecked_extrinsic_size as *mut u32,
)
};

ensure!(result == sgx_status_t::SGX_SUCCESS, Error::Sgx(result));
ensure!(retval == sgx_status_t::SGX_SUCCESS, Error::Sgx(retval));

Ok(unchecked_extrinsic.to_vec())
Ok(Vec::from(&unchecked_extrinsic[..unchecked_extrinsic_size as usize]))
}

fn generate_dcap_ra_quote(&self, skip_ra: bool) -> EnclaveResult<Vec<u8>> {
Expand Down Expand Up @@ -250,7 +254,7 @@ mod impl_ffi {
trace!("Generating dcap_ra_extrinsic with URL: {}", w_url);

let mut unchecked_extrinsic: Vec<u8> = vec![0u8; EXTRINSIC_MAX_SIZE];

let mut unchecked_extrinsic_size: u32 = 0;
let url = w_url.encode();

let result = unsafe {
Expand All @@ -261,6 +265,7 @@ mod impl_ffi {
url.len() as u32,
unchecked_extrinsic.as_mut_ptr(),
unchecked_extrinsic.len() as u32,
&mut unchecked_extrinsic_size as *mut u32,
skip_ra.into(),
quoting_enclave_target_info.as_ref(),
quote_size.as_ref(),
Expand All @@ -270,7 +275,7 @@ mod impl_ffi {
ensure!(result == sgx_status_t::SGX_SUCCESS, Error::Sgx(result));
ensure!(retval == sgx_status_t::SGX_SUCCESS, Error::Sgx(retval));

Ok(unchecked_extrinsic)
Ok(Vec::from(&unchecked_extrinsic[..unchecked_extrinsic_size as usize]))
}

fn generate_register_quoting_enclave_extrinsic(
Expand All @@ -279,6 +284,7 @@ mod impl_ffi {
) -> EnclaveResult<Vec<u8>> {
let mut retval = sgx_status_t::SGX_SUCCESS;
let mut unchecked_extrinsic: Vec<u8> = vec![0u8; EXTRINSIC_MAX_SIZE];
let mut unchecked_extrinsic_size: u32 = 0;

trace!("Generating register quoting enclave");

Expand All @@ -291,6 +297,7 @@ mod impl_ffi {
collateral_ptr,
unchecked_extrinsic.as_mut_ptr(),
unchecked_extrinsic.len() as u32,
&mut unchecked_extrinsic_size as *mut u32,
)
};
let free_status = unsafe { sgx_ql_free_quote_verification_collateral(collateral_ptr) };
Expand All @@ -301,12 +308,13 @@ mod impl_ffi {
Error::SgxQuote(free_status)
);

Ok(unchecked_extrinsic)
Ok(Vec::from(&unchecked_extrinsic[..unchecked_extrinsic_size as usize]))
}

fn generate_register_tcb_info_extrinsic(&self, fmspc: Fmspc) -> EnclaveResult<Vec<u8>> {
let mut retval = sgx_status_t::SGX_SUCCESS;
let mut unchecked_extrinsic: Vec<u8> = vec![0u8; EXTRINSIC_MAX_SIZE];
let mut unchecked_extrinsic_size: u32 = 0;

trace!("Generating tcb_info registration");

Expand All @@ -319,6 +327,7 @@ mod impl_ffi {
collateral_ptr,
unchecked_extrinsic.as_mut_ptr(),
unchecked_extrinsic.len() as u32,
&mut unchecked_extrinsic_size as *mut u32,
)
};
let free_status = unsafe { sgx_ql_free_quote_verification_collateral(collateral_ptr) };
Expand All @@ -329,7 +338,7 @@ mod impl_ffi {
Error::SgxQuote(free_status)
);

Ok(unchecked_extrinsic)
Ok(Vec::from(&unchecked_extrinsic[..unchecked_extrinsic_size as usize]))
}

fn dump_ias_ra_cert_to_disk(&self) -> EnclaveResult<()> {
Expand Down
20 changes: 12 additions & 8 deletions core-primitives/enclave-api/src/teeracle_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ mod impl_ffi {
crypto_currency, fiat_currency
);
let mut retval = sgx_status_t::SGX_SUCCESS;
let response_len = 8192;
let mut response: Vec<u8> = vec![0u8; response_len as usize];
let response_max_len = 8192;
let mut response: Vec<u8> = vec![0u8; response_max_len as usize];
let mut response_len: u32 = 0;

let crypto_curr = crypto_currency.encode();
let fiat_curr = fiat_currency.encode();
Expand All @@ -64,14 +65,15 @@ mod impl_ffi {
fiat_curr.as_ptr(),
fiat_curr.len() as u32,
response.as_mut_ptr(),
response_len,
response_max_len,
&mut response_len as *mut u32,
)
};

ensure!(res == sgx_status_t::SGX_SUCCESS, Error::Sgx(res));
ensure!(retval == sgx_status_t::SGX_SUCCESS, Error::Sgx(retval));

Ok(response)
Ok(Vec::from(&response[..response_len as usize]))
}
fn update_weather_data_xt(
&self,
Expand All @@ -83,8 +85,9 @@ mod impl_ffi {
latitude, longitude
);
let mut retval = sgx_status_t::SGX_SUCCESS;
let response_len = 8192;
let mut response: Vec<u8> = vec![0u8; response_len as usize];
let response_max_len = 8192;
let mut response: Vec<u8> = vec![0u8; response_max_len as usize];
let mut response_len: u32 = 0;

let longitude_encoded: Vec<u8> = longitude.encode();
let latitude_encoded: Vec<u8> = latitude.encode();
Expand All @@ -98,13 +101,14 @@ mod impl_ffi {
latitude_encoded.as_ptr(),
latitude_encoded.len() as u32,
response.as_mut_ptr(),
response_len,
response_max_len,
&mut response_len as *mut u32,
)
};

ensure!(res == sgx_status_t::SGX_SUCCESS, Error::Sgx(res));
ensure!(retval == sgx_status_t::SGX_SUCCESS, Error::Sgx(retval));
Ok(response)
Ok(Vec::from(&response[..response_len as usize]))
}
}
}
15 changes: 13 additions & 2 deletions core-primitives/utils/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
use alloc::vec::Vec;

/// Fills a given buffer with data and the left over buffer space with white spaces.
/// Throw an error if the buffer size is not enough to hold `data`,
/// return the length of `data` otherwise.
pub fn write_slice_and_whitespace_pad(
writable: &mut [u8],
data: Vec<u8>,
) -> Result<(), BufferError> {
) -> Result<usize, BufferError> {
if data.len() > writable.len() {
return Err(BufferError::InsufficientBufferSize {
actual: writable.len(),
Expand All @@ -34,7 +36,7 @@ pub fn write_slice_and_whitespace_pad(
left.clone_from_slice(&data);
// fill the right side with whitespace
right.iter_mut().for_each(|x| *x = 0x20);
Ok(())
Ok(data.len())
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
Expand All @@ -47,6 +49,15 @@ mod tests {
use super::*;
use alloc::vec;

#[test]
fn write_slice_and_whitespace_pad_works() {
let mut writable = vec![0; 32];
let data = vec![1; 30];
assert_eq!(write_slice_and_whitespace_pad(&mut writable, data), Ok(30));
assert_eq!(&writable[..30], vec![1; 30]);
assert_eq!(&writable[30..], vec![0x20; 2]);
}

#[test]
fn write_slice_and_whitespace_pad_returns_error_if_buffer_too_small() {
let mut writable = vec![0; 32];
Expand Down
21 changes: 14 additions & 7 deletions enclave-runtime/Enclave.edl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ enclave {

public sgx_status_t generate_ias_ra_extrinsic(
[in, size=w_url_size] uint8_t* w_url, uint32_t w_url_size,
[out, size=unchecked_extrinsic_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_size,
[out, size=unchecked_extrinsic_max_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_max_size,
[out] uint32_t* unchecked_extrinsic_size,
int skip_ra
);
public sgx_status_t generate_dcap_ra_quote(
Expand All @@ -111,37 +112,43 @@ enclave {
public sgx_status_t generate_dcap_ra_extrinsic_from_quote(
[in, size=w_url_size] uint8_t* w_url, uint32_t w_url_size,
[in, size=quote_size] uint8_t* quote, uint32_t quote_size,
[out, size=unchecked_extrinsic_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_size
[out, size=unchecked_extrinsic_max_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_max_size,
[out] uint32_t* unchecked_extrinsic_size
);

public sgx_status_t generate_dcap_ra_extrinsic(
[in, size=w_url_size] uint8_t* w_url, uint32_t w_url_size,
[out, size=unchecked_extrinsic_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_size,
[out, size=unchecked_extrinsic_max_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_max_size,
[out] uint32_t* unchecked_extrinsic_size,
int skip_ra,
[in] const sgx_target_info_t* quoting_enclave_target_info,
[in] uint32_t* quote_size
);

public sgx_status_t generate_register_quoting_enclave_extrinsic(
[in] const sgx_ql_qve_collateral_t *p_quote_collateral,
[out, size=unchecked_extrinsic_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_size
[out, size=unchecked_extrinsic_max_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_max_size,
[out] uint32_t* unchecked_extrinsic_size
);

public sgx_status_t generate_register_tcb_info_extrinsic(
[in] const sgx_ql_qve_collateral_t *p_quote_collateral,
[out, size=unchecked_extrinsic_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_size
[out, size=unchecked_extrinsic_max_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_max_size,
[out] uint32_t* unchecked_extrinsic_size
);

public sgx_status_t update_market_data_xt(
[in, size=crypto_currency_size] uint8_t* crypto_currency, uint32_t crypto_currency_size,
[in, size=fiat_currency_size] uint8_t* fiat_currency, uint32_t fiat_currency_size,
[out, size=unchecked_extrinsic_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_size
[out, size=unchecked_extrinsic_max_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_max_size,
[out] uint32_t* unchecked_extrinsic_size
);

public sgx_status_t update_weather_data_xt(
[in, size=weather_info_logitude_size] uint8_t* weather_info_logitude, uint32_t weather_info_logitude_size,
[in, size=weather_info_latitude_size] uint8_t* weather_info_latitude, uint32_t weather_info_latitude_size,
[out, size=unchecked_extrinsic_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_size
[out, size=unchecked_extrinsic_max_size] uint8_t* unchecked_extrinsic, uint32_t unchecked_extrinsic_max_size,
[out] uint32_t* unchecked_extrinsic_size
);

public sgx_status_t dump_ias_ra_cert_to_disk();
Expand Down
Loading

0 comments on commit e40355f

Please sign in to comment.