1extern crate dcap_ql_sys;
2#[cfg(not(feature = "link"))]
3extern crate libc;
4
5extern crate sgxs_loaders;
6
7use anyhow::Error;
8use anyhow::anyhow;
9use num_traits::FromPrimitive;
10
11pub use self::dcap_ql_sys::Quote3Error;
12use sgx_isa::{Report, Targetinfo};
13use self::sgxs_loaders::sgx_enclave_common::dl::os::unix::Library as Dl;
14use self::sgxs_loaders::sgx_enclave_common::Library as EnclaveCommonLibrary;
15
16#[cfg(feature = "link")]
17use self::dcap_ql_sys::{get_quote, get_quote_size, get_target_info};
18#[cfg(not(feature = "link"))]
19use self::dl::{get_quote, get_quote_size, get_target_info};
20
21#[cfg(not(feature = "link"))]
22mod dl;
23
24fn err_code_to_result(err: u32) -> Result<(), Quote3Error> {
25 match Quote3Error::from_u32(err) {
26 Some(Quote3Error::Success) => Ok(()),
27 Some(e) => Err(e),
28 _ => Err(Quote3Error::InvalidParameter),
29 }
30}
31
32pub fn target_info() -> Result<Targetinfo, Quote3Error> {
37 unsafe {
38 let mut targetinfo = Targetinfo::default();
39 err_code_to_result(get_target_info(&mut targetinfo))?;
40 Ok(targetinfo)
41 }
42}
43
44pub fn quote(report: &Report) -> Result<Vec<u8>, Quote3Error> {
49 unsafe {
50 let mut quote_size = 0;
51 err_code_to_result(get_quote_size(&mut quote_size))?;
52
53 let mut quote = vec![0; quote_size as _];
54 err_code_to_result(get_quote(&report, quote_size, quote.as_mut_ptr()))?;
55 Ok(quote)
56 }
57}
58
59pub fn is_loaded() -> bool {
64 #[cfg(not(feature = "link"))]
65 {
66 dl::load().is_ok()
67 }
68 #[cfg(feature = "link")]
69 {
70 true
71 }
72}
73
74pub fn enclave_loader() -> Result<EnclaveCommonLibrary, Error> {
79 #[cfg(not(feature = "link"))]
80 dl::load().map_err(|e| anyhow!(e))?;
81 let lib = EnclaveCommonLibrary::load(Some(Dl::this().into()))
86 .or(EnclaveCommonLibrary::load(None))?;
87 Ok(lib.build())
88}