Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
feat(dh): fix uenclave tests
Browse files Browse the repository at this point in the history
  • Loading branch information
longtomjr committed May 18, 2021
1 parent 2684561 commit 6a3b291
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 32 deletions.
13 changes: 10 additions & 3 deletions rtc_uenclave/src/ecalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ pub struct EnclaveReportResult {

/// Error returned when the enclave fails to create a report
pub type CreateReportError = EcallError<CreateReportResult>;
pub trait RtcEcalls: RtcEnclaveEcalls {

pub trait RtcEcalls {
fn create_report(
&self,
eid: sgx_enclave_id_t,
qe_target_info: &sgx_target_info_t,
) -> Result<EnclaveReportResult, CreateReportError>;
}

impl<T: RtcEnclaveEcalls> RtcEcalls for T {
fn create_report(
&self,
eid: sgx_enclave_id_t,
Expand Down Expand Up @@ -57,8 +66,6 @@ pub trait RtcEcalls: RtcEnclaveEcalls {
}
}

impl<T: RtcEnclaveEcalls> RtcEcalls for T {}

#[cfg(test)]
mod test {
use super::*;
Expand Down
92 changes: 63 additions & 29 deletions rtc_uenclave/src/rtc_enclave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,22 @@ pub struct EnclaveConfig {
///
/// This struct contains the basic functionality required from all RTC enclaves
#[cfg_attr(not(test), derive(Debug))]
pub(crate) struct RtcEnclave<
pub(crate) struct RtcEnclave<TCfg, TEcalls>
where
TCfg: Borrow<EnclaveConfig>,
TEcalls: RtcEcalls + ResponderSys + 'static,
> {
TEcalls: RtcEcalls + Default + ResponderSys + 'static,
{
pub(crate) base_enclave: SgxEnclave,
pub(crate) quoting_enclave: QuotingEnclave,
pub(crate) attestation_client: AzureAttestationClient<ureq::Agent>,
pub(crate) config: TCfg,
ecalls: TEcalls,
}

impl<TCfg: Borrow<EnclaveConfig>, TEcalls: RtcEcalls + ResponderSys + 'static>
RtcEnclave<TCfg, TEcalls>
impl<TCfg, TEcalls> RtcEnclave<TCfg, TEcalls>
where
TCfg: Borrow<EnclaveConfig>,
TEcalls: RtcEcalls + Default + ResponderSys + 'static,
{
/// Creates a new enclave instance with the provided configuration
pub fn init(cfg: TCfg) -> Result<Self, sgx_status_t> {
Expand Down Expand Up @@ -210,12 +213,49 @@ mod tests {
use proptest::collection::size_range;
use proptest::prelude::*;
use rtc_ecalls::MockRtcEnclaveEcalls;
use rtc_types::dh::{ExchangeReportResult, SessionRequestResult};
use rtc_types::CreateReportResult;
use rtc_types::EnclaveHeldData;
use rtc_types::{ENCLAVE_HELD_DATA_SIZE, RSA3072_PKCS8_DER_SIZE};
use simple_asn1::{to_der, ASN1Block, BigInt, BigUint, OID};
use std::convert::TryInto;

mock! {
TEcalls {}

impl RtcEcalls for TEcalls {
fn create_report(
&self,
eid: sgx_enclave_id_t,
qe_target_info: &sgx_target_info_t,
) -> Result<EnclaveReportResult, CreateReportError>;
}

impl ResponderSys for TEcalls {
unsafe fn rtc_session_request(
&self,
eid: sgx_enclave_id_t,
retval: *mut SessionRequestResult,
src_enclave_id: sgx_enclave_id_t,
) -> sgx_status_t;

unsafe fn rtc_exchange_report(
&self,
eid: sgx_enclave_id_t,
retval: *mut ExchangeReportResult,
src_enclave_id: sgx_enclave_id_t,
dh_msg2_ptr: *const sgx_dh_msg2_t,
) -> sgx_status_t;

unsafe fn rtc_end_session(
&self,
eid: sgx_enclave_id_t,
retval: *mut sgx_status_t,
src_enclave_id: sgx_enclave_id_t,
) -> sgx_status_t;
}
}

#[test]
fn dcap_azure_attestation_works() {
let sut = {
Expand All @@ -242,24 +282,21 @@ mod tests {
let qe_target_info = sgx_target_info_t::default();
let ehd = [2; ENCLAVE_HELD_DATA_SIZE];
let report = sgx_report_t::default();
let mut sys_mock = MockRtcEnclaveEcalls::default();
sys_mock
.expect_enclave_create_report()
.returning(move |_, ret, _, key, rep| {
unsafe {
*rep = report;
(*key).copy_from_slice(&ehd);
*ret = CreateReportResult::Success;
}
sgx_status_t::SGX_SUCCESS
});

let mut tecalls_mock = MockTEcalls::default();
tecalls_mock
.expect_create_report()
.return_const(Ok(EnclaveReportResult {
enclave_report: report,
enclave_held_data: ehd,
}));

RtcEnclave {
base_enclave: mock_be,
quoting_enclave: mock_qe,
attestation_client: mock_aa_client,
config: EnclaveConfig::default(),
ecalls: sys_mock,
ecalls: tecalls_mock,
}
};

Expand Down Expand Up @@ -360,24 +397,21 @@ mod tests {

let eid = 12u64;
let qe_target_info = sgx_target_info_t::default();
let mut sys_mock = MockRtcEnclaveEcalls::default();
sys_mock
.expect_enclave_create_report()
.returning(move |_, ret, _, key, rep| {
unsafe {
*rep = report;
(*key).copy_from_slice(&ehd);
*ret = CreateReportResult::Success;
}
sgx_status_t::SGX_SUCCESS
});

let mut tecalls_mock = MockTEcalls::default();
tecalls_mock
.expect_create_report()
.return_const(Ok(EnclaveReportResult {
enclave_report: report,
enclave_held_data: ehd,
}));

let sut = RtcEnclave{
base_enclave: mock,
quoting_enclave: QuotingEnclave::default(),
attestation_client: AzureAttestationClient::<ureq::Agent>::default(),
config: EnclaveConfig::default(),
ecalls: sys_mock,
ecalls: tecalls_mock,
};

let res = sut.create_report(&qe_ti).unwrap();
Expand Down

0 comments on commit 6a3b291

Please sign in to comment.