Skip to content

Commit

Permalink
warnings fix
Browse files Browse the repository at this point in the history
  • Loading branch information
valdok committed Feb 26, 2024
1 parent f7cf357 commit e5edfac
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 239 deletions.
132 changes: 65 additions & 67 deletions cosmwasm/enclaves/execute/src/registration/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,41 +295,40 @@ pub fn get_mr_enclave() -> [u8; 32] {

#[cfg(feature = "SGX_MODE_HW")]
pub fn verify_quote_ecdsa(
vQuote : &Vec<u8>,
vCol : &Vec<u8>,
nTime: i64,
vec_quote : &Vec<u8>,
vec_coll : &Vec<u8>,
time_s: i64,
) -> Result<(sgx_report_body_t, sgx_ql_qv_result_t), sgx_status_t>
{
let mut qe_report: sgx_ql_qe_report_info_t = sgx_ql_qe_report_info_t::default();
let mut pSupp: [u8; 5000] = [0; 5000];
let mut nSupp: u32 = 0;
let mut nExpTime: i64 = 0;
let mut nExpStatus: u32 = 0;
let mut qvResult: sgx_ql_qv_result_t = sgx_ql_qv_result_t::default();
let mut p_supp: [u8; 5000] = [0; 5000];
let mut n_supp: u32 = 0;
let mut exp_time_s: i64 = 0;
let mut exp_status: u32 = 0;
let mut qv_result: sgx_ql_qv_result_t = sgx_ql_qv_result_t::default();
let mut rt: sgx_status_t = sgx_status_t::default();
let mut ti: sgx_target_info_t = sgx_target_info_t::default();

let mut ti: sgx_target_info_t = sgx_target_info_t::default();
unsafe {
sgx_self_target(&mut ti)
};

let mut res = unsafe {
let res = unsafe {
ocall_verify_quote_ecdsa(
&mut rt as *mut sgx_status_t,
vQuote.as_ptr(),
vQuote.len() as u32,
vCol.as_ptr(),
vCol.len() as u32,
vec_quote.as_ptr(),
vec_quote.len() as u32,
vec_coll.as_ptr(),
vec_coll.len() as u32,
&ti,
nTime,
time_s,
&mut qe_report,
pSupp.as_mut_ptr(),
pSupp.len() as u32,
&mut nSupp,
&mut nExpTime,
&mut nExpStatus,
&mut qvResult)
p_supp.as_mut_ptr(),
p_supp.len() as u32,
&mut n_supp,
&mut exp_time_s,
&mut exp_status,
&mut qv_result)
};

if res != sgx_status_t::SGX_SUCCESS {
Expand All @@ -339,55 +338,55 @@ pub fn verify_quote_ecdsa(
return Err(rt);
}

match qvResult {
match qv_result {
sgx_ql_qv_result_t::SGX_QL_QV_RESULT_OK => {},
sgx_ql_qv_result_t::SGX_QL_QV_RESULT_SW_HARDENING_NEEDED => {},
_ => {
trace!("Quote verification result: {}", qvResult);
trace!("Quote verification result: {}", qv_result);
return Err(sgx_status_t::SGX_ERROR_UNEXPECTED);
}
};

// verify the qve report
if nTime != 0 {
nExpTime = nTime; // insist on our time, if supplied
if time_s != 0 {
exp_time_s = time_s; // insist on our time, if supplied
}

let qve_isvsvn_threshold: sgx_isv_svn_t = 3;
let dcap_ret : sgx_quote3_error_t = unsafe { sgx_tvl_verify_qve_report_and_identity(
vQuote.as_ptr(),
vQuote.len() as u32,
vec_quote.as_ptr(),
vec_quote.len() as u32,
&qe_report,
nExpTime,
nExpStatus,
qvResult,
pSupp.as_ptr(),
nSupp,
exp_time_s,
exp_status,
qv_result,
p_supp.as_ptr(),
n_supp,
qve_isvsvn_threshold) };

if dcap_ret != sgx_quote3_error_t::SGX_QL_SUCCESS {
trace!("QVE report verification result: {}", dcap_ret);
return Err(sgx_status_t::SGX_ERROR_UNEXPECTED)
}

trace!("nSupp = {}", nSupp);
trace!("nExpTime = {}", nExpTime);
trace!("nExpStatus = {}", nExpStatus);
trace!("qvResult = {}", qvResult);
trace!("n_supp = {}", n_supp);
trace!("exp_time_s = {}", exp_time_s);
trace!("exp_status = {}", exp_status);
trace!("qv_result = {}", qv_result);

if vQuote.len() < mem::size_of::<sgx_quote_t>() {
if vec_quote.len() < mem::size_of::<sgx_quote_t>() {
trace!("Quote too small");
return Err(sgx_status_t::SGX_ERROR_UNEXPECTED);
}

let my_pQuote = vQuote.as_ptr() as *const sgx_quote_t;
let report_body = unsafe { (*my_pQuote).report_body };
let my_p_quote = vec_quote.as_ptr() as *const sgx_quote_t;
let report_body = unsafe { (*my_p_quote).report_body };

trace!("body.mr_signer = {:?}", report_body.mr_signer.m);
trace!("body.mr_enclave = {:?}", report_body.mr_enclave.m);
trace!("body.report_data = {:?}", report_body.report_data.d);

Ok((report_body, qvResult))
Ok((report_body, qv_result))
}

#[cfg(feature = "SGX_MODE_HW")]
Expand Down Expand Up @@ -430,20 +429,20 @@ pub fn get_quote_ecdsa(
)
};

if rt != sgx_status_t::SGX_SUCCESS {
trace!("sgx_create_report = {}", rt);
if res != sgx_status_t::SGX_SUCCESS {
trace!("sgx_create_report = {}", res);
}

let mut vQuote : Vec<u8> = Vec::new();
vQuote.resize(quote_size as usize, 0);
let mut vec_quote : Vec<u8> = Vec::new();
vec_quote.resize(quote_size as usize, 0);

res = unsafe {

ocall_get_quote_ecdsa(
&mut rt as *mut sgx_status_t,
&my_report,
vQuote.as_mut_ptr(),
vQuote.len() as u32)
vec_quote.as_mut_ptr(),
vec_quote.len() as u32)
};

if res != sgx_status_t::SGX_SUCCESS {
Expand All @@ -454,18 +453,18 @@ pub fn get_quote_ecdsa(
trace!("rt = {}", rt);
}

let mut vCol : Vec<u8> = Vec::new();
vCol.resize(0x4000, 0);
let mut nCol : u32 = 0;
let mut vec_coll : Vec<u8> = Vec::new();
vec_coll.resize(0x4000, 0);
let mut size_coll : u32 = 0;

let mut res = unsafe {
let res = unsafe {
ocall_get_quote_ecdsa_collateral(
&mut rt as *mut sgx_status_t,
vQuote.as_ptr(),
vQuote.len() as u32,
vCol.as_mut_ptr(),
vCol.len() as u32,
&mut nCol)
vec_quote.as_ptr(),
vec_quote.len() as u32,
vec_coll.as_mut_ptr(),
vec_coll.len() as u32,
&mut size_coll)
};

if res != sgx_status_t::SGX_SUCCESS {
Expand All @@ -476,35 +475,34 @@ pub fn get_quote_ecdsa(
trace!("rt = {}", rt);
}

trace!("Collateral size = {}", nCol);
trace!("Collateral size = {}", size_coll);

let bAgain = nCol > vCol.len() as u32;
vCol.resize(nCol as usize, 0);
let call_again = size_coll > vec_coll.len() as u32;
vec_coll.resize(size_coll as usize, 0);

//if bAgain
if call_again
{
unsafe {
ocall_get_quote_ecdsa_collateral(
&mut rt as *mut sgx_status_t,
vQuote.as_ptr(),
vQuote.len() as u32,
vCol.as_mut_ptr(),
vCol.len() as u32,
&mut nCol)
vec_quote.as_ptr(),
vec_quote.len() as u32,
vec_coll.as_mut_ptr(),
vec_coll.len() as u32,
&mut size_coll)
};
}

if res == sgx_status_t::SGX_SUCCESS
{
// test self
let report_body = match verify_quote_ecdsa(&vQuote, &vCol, 0) {
match verify_quote_ecdsa(&vec_quote, &vec_coll, 0) {
Ok(r) => {
trace!("Self quote verified ok");
if r.1 != sgx_ql_qv_result_t::SGX_QL_QV_RESULT_OK {
// TODO: strict policy wrt own quote verification
trace!("WARNING: {}", r.1);
}
r.0
}
Err(e) => {
trace!("Self quote verification failed: {}", e);
Expand All @@ -514,7 +512,7 @@ pub fn get_quote_ecdsa(
};


Ok((vQuote, vCol))
Ok((vec_quote, vec_coll))

}

Expand Down
2 changes: 1 addition & 1 deletion cosmwasm/enclaves/execute/src/registration/ocalls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use sgx_types::{
sgx_epid_group_id_t, sgx_quote_nonce_t, sgx_quote_sign_type_t, sgx_report_t, sgx_spid_t,
sgx_status_t, sgx_target_info_t, sgx_ql_qe_report_info_t, sgx_isv_svn_t, sgx_ql_qv_result_t
sgx_status_t, sgx_target_info_t, sgx_ql_qe_report_info_t, sgx_ql_qv_result_t
};

extern "C" {
Expand Down
36 changes: 18 additions & 18 deletions cosmwasm/enclaves/execute/src/registration/offchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,23 +387,23 @@ unsafe fn ecall_get_attestation_report_dcap(
kp: &KeyPair,
) -> Result<(Vec<u8>, Vec<u8>), sgx_status_t>
{
let (vQuote, vColl) = match get_quote_ecdsa(&kp.get_pubkey()) {
let (vec_quote, vec_coll) = match get_quote_ecdsa(&kp.get_pubkey()) {
Ok(r) => r,
Err(e) => {
warn!("Error creating attestation report");
return Err(e);
}
};

if let Err(status) = write_to_untrusted(&vQuote, ATTESTATION_DCAP_PATH.as_str()) {
if let Err(status) = write_to_untrusted(&vec_quote, ATTESTATION_DCAP_PATH.as_str()) {
return Err(status);
}

if let Err(status) = write_to_untrusted(&vColl, COLLATERAL_DCAP_PATH.as_str()) {
if let Err(status) = write_to_untrusted(&vec_coll, COLLATERAL_DCAP_PATH.as_str()) {
return Err(status);
}

return Ok((vQuote, vColl));
return Ok((vec_quote, vec_coll));
}


Expand Down Expand Up @@ -439,35 +439,35 @@ pub unsafe extern "C" fn ecall_get_attestation_report(
let mut size_dcap_c : u32 = 0;

let res_epid = ecall_get_attestation_report_epid(api_key, api_key_len, &kp);
if let Ok(ref vCert) = res_epid {
size_epid = vCert.len() as u32;
if let Ok(ref vec_cert) = res_epid {
size_epid = vec_cert.len() as u32;
}

let res_dcap = ecall_get_attestation_report_dcap(&kp);
if let Ok((ref vQuote, ref vColl)) = res_dcap {
size_dcap_q = vQuote.len() as u32;
size_dcap_c = vColl.len() as u32;
if let Ok((ref vec_quote, ref vec_coll)) = res_dcap {
size_dcap_q = vec_quote.len() as u32;
size_dcap_c = vec_coll.len() as u32;
}

let mut fOut = match File::create(CERT_COMBINED_PATH.as_str()) {
let mut f_out = match File::create(CERT_COMBINED_PATH.as_str()) {
Ok(f) => f,
Err(e) => {
error!("failed to create file {}", e);
return sgx_status_t::SGX_ERROR_UNEXPECTED;
}
};

fOut.write(&(size_epid as u32).to_le_bytes());
fOut.write(&(size_dcap_q as u32).to_le_bytes());
fOut.write(&(size_dcap_c as u32).to_le_bytes());
f_out.write(&(size_epid as u32).to_le_bytes());
f_out.write(&(size_dcap_q as u32).to_le_bytes());
f_out.write(&(size_dcap_c as u32).to_le_bytes());

if let Ok(ref vCert) = res_epid {
fOut.write_all(vCert.as_slice());
if let Ok(ref vec_cert) = res_epid {
f_out.write_all(vec_cert.as_slice());
}

if let Ok((vQuote, vColl)) = res_dcap {
fOut.write_all(vQuote.as_slice());
fOut.write_all(vColl.as_slice());
if let Ok((vec_quote, vec_coll)) = res_dcap {
f_out.write_all(vec_quote.as_slice());
f_out.write_all(vec_coll.as_slice());
}

if (size_epid == 0) && (size_dcap_q == 0) {
Expand Down
Loading

0 comments on commit e5edfac

Please sign in to comment.