Skip to content

Commit ba3fc98

Browse files
authored
Merge pull request #1655 from scrtlabs/dcap-11
Dcap 11
2 parents 9dfdf8b + 99145a3 commit ba3fc98

File tree

2 files changed

+79
-29
lines changed

2 files changed

+79
-29
lines changed

cosmwasm/enclaves/execute/src/registration/attestation.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ pub fn get_quote_ecdsa(_pub_k: &[u8; 32]) -> Result<(Vec<u8>, Vec<u8>), sgx_stat
438438
}
439439

440440
#[cfg(feature = "SGX_MODE_HW")]
441-
pub fn get_quote_ecdsa(pub_k: &[u8; 32]) -> Result<(Vec<u8>, Vec<u8>), sgx_status_t> {
441+
pub fn get_quote_ecdsa_untested(pub_k: &[u8; 32]) -> Result<(Vec<u8>, Vec<u8>), sgx_status_t> {
442442
let mut qe_target_info = sgx_target_info_t::default();
443443
let mut quote_size: u32 = 0;
444444
let mut rt: sgx_status_t = sgx_status_t::default();
@@ -527,6 +527,13 @@ pub fn get_quote_ecdsa(pub_k: &[u8; 32]) -> Result<(Vec<u8>, Vec<u8>), sgx_statu
527527
}
528528
}
529529

530+
Ok((vec_quote, vec_coll))
531+
}
532+
533+
#[cfg(feature = "SGX_MODE_HW")]
534+
pub fn get_quote_ecdsa(pub_k: &[u8; 32]) -> Result<(Vec<u8>, Vec<u8>), sgx_status_t> {
535+
let (vec_quote, vec_coll) = get_quote_ecdsa_untested(pub_k)?;
536+
530537
// test self
531538
match verify_quote_ecdsa(&vec_quote, &vec_coll, 0) {
532539
Ok(r) => {

cosmwasm/enclaves/execute/src/registration/check_patch_level.rs

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ use crate::registration::attestation::create_attestation_report;
1515
use crate::registration::cert::verify_quote_status;
1616

1717
#[cfg(feature = "SGX_MODE_HW")]
18-
use crate::registration::offchain::get_attestation_report_dcap;
18+
use crate::registration::attestation::get_quote_ecdsa_untested;
19+
20+
#[cfg(feature = "SGX_MODE_HW")]
21+
use crate::registration::attestation::verify_quote_ecdsa;
22+
23+
#[cfg(feature = "SGX_MODE_HW")]
24+
use enclave_utils::storage::write_to_untrusted;
25+
26+
#[cfg(feature = "SGX_MODE_HW")]
27+
use crate::sgx_types::sgx_ql_qv_result_t;
1928

2029
#[cfg(not(feature = "epid_whitelist_disabled"))]
2130
use crate::registration::cert::check_epid_gid_is_whitelisted;
@@ -35,11 +44,37 @@ pub unsafe extern "C" fn ecall_check_patch_level(
3544
panic!("unimplemented")
3645
}
3746

38-
/// # Safety
39-
/// Don't forget to check the input length of api_key_len
40-
#[no_mangle]
4147
#[cfg(feature = "SGX_MODE_HW")]
42-
pub unsafe extern "C" fn ecall_check_patch_level(
48+
unsafe fn check_patch_level_dcap(pub_k: &[u8; 32]) -> NodeAuthResult {
49+
match get_quote_ecdsa_untested(pub_k) {
50+
Ok((vec_quote, vec_coll)) => {
51+
match verify_quote_ecdsa(&vec_quote, &vec_coll, 0) {
52+
Ok(r) => {
53+
if r.1 != sgx_ql_qv_result_t::SGX_QL_QV_RESULT_OK {
54+
println!("WARNING: {}", r.1);
55+
}
56+
57+
println!("DCAP attestation obtained and verified ok");
58+
return NodeAuthResult::Success;
59+
}
60+
Err(e) => {
61+
println!("DCAP quote obtained, but failed to verify it: {}", e);
62+
63+
let _ = write_to_untrusted(&vec_quote, "dcap_quote.bin");
64+
let _ = write_to_untrusted(&vec_coll, "dcap_collateral.bin");
65+
}
66+
};
67+
}
68+
Err(e) => {
69+
println!("Failed to obtain DCAP attestation: {}", e);
70+
}
71+
}
72+
NodeAuthResult::InvalidCert
73+
}
74+
75+
#[cfg(feature = "SGX_MODE_HW")]
76+
unsafe fn check_patch_level_epid(
77+
pub_k: &[u8; 32],
4378
api_key: *const u8,
4479
api_key_len: u32,
4580
) -> NodeAuthResult {
@@ -51,29 +86,14 @@ pub unsafe extern "C" fn ecall_check_patch_level(
5186

5287
let api_key_slice = slice::from_raw_parts(api_key, api_key_len as usize);
5388

54-
// CREATE THE ATTESTATION REPORT
55-
// generate temporary key for attestation
56-
let temp_key_result = enclave_crypto::KeyPair::new().unwrap();
57-
58-
let res_dcap = unsafe { get_attestation_report_dcap(&temp_key_result) };
59-
if res_dcap.is_ok() {
60-
println!("DCAP attestation ok");
61-
return NodeAuthResult::Success;
62-
}
63-
64-
let signed_report = match create_attestation_report(
65-
&temp_key_result.get_pubkey(),
66-
SIGNATURE_TYPE,
67-
api_key_slice,
68-
None,
69-
true,
70-
) {
71-
Ok(r) => r,
72-
Err(_e) => {
73-
error!("Error creating attestation report");
74-
return NodeAuthResult::InvalidCert;
75-
}
76-
};
89+
let signed_report =
90+
match create_attestation_report(pub_k, SIGNATURE_TYPE, api_key_slice, None, true) {
91+
Ok(r) => r,
92+
Err(_e) => {
93+
error!("Error creating attestation report");
94+
return NodeAuthResult::InvalidCert;
95+
}
96+
};
7797

7898
let payload: String = serde_json::to_string(&signed_report)
7999
.map_err(|_| {
@@ -151,3 +171,26 @@ pub unsafe extern "C" fn ecall_check_patch_level(
151171
_ => NodeAuthResult::Success,
152172
}
153173
}
174+
175+
/// # Safety
176+
/// Don't forget to check the input length of api_key_len
177+
#[no_mangle]
178+
#[cfg(feature = "SGX_MODE_HW")]
179+
pub unsafe extern "C" fn ecall_check_patch_level(
180+
api_key: *const u8,
181+
api_key_len: u32,
182+
) -> NodeAuthResult {
183+
let temp_key_result = enclave_crypto::KeyPair::new().unwrap();
184+
185+
let res1 = check_patch_level_dcap(&temp_key_result.get_pubkey());
186+
let res2 = check_patch_level_epid(&temp_key_result.get_pubkey(), api_key, api_key_len);
187+
188+
println!("DCAP attestation: {}", res1);
189+
println!("EPID attestation: {}", res2);
190+
191+
if NodeAuthResult::Success == res1 {
192+
return res1;
193+
}
194+
195+
res2
196+
}

0 commit comments

Comments
 (0)