oasis_core_runtime/
lib.rs

1//! Oasis Core runtime SDK.
2//!
3//! # Examples
4//!
5//! To create a minimal runtime that doesn't expose any APIs to the
6//! outside world, you need to call the `start_runtime` function:
7//! ```rust,ignore
8//! oasis_core_runtime::start_runtime(Some(Box::new(reg)), config);
9//! ```
10//!
11//! This will start the required services needed to communicate with
12//! the worker host.
13#![feature(test)]
14#![feature(arbitrary_self_types)]
15#![feature(const_option)]
16
17use lazy_static::lazy_static;
18#[cfg(target_env = "sgx")]
19use sgx_isa::{AttributesFlags, Report};
20
21#[cfg_attr(test, macro_use)]
22extern crate base64_serde;
23
24#[macro_use]
25pub mod common;
26pub mod app;
27mod attestation;
28pub mod cache;
29pub mod config;
30pub mod consensus;
31pub mod dispatcher;
32pub mod enclave_rpc;
33pub mod future;
34pub mod host;
35pub mod identity;
36pub mod init;
37pub mod policy;
38pub mod protocol;
39pub mod storage;
40pub mod transaction;
41pub mod types;
42
43use common::{
44    sgx::{EnclaveIdentity, MrSigner},
45    version::{Version, PROTOCOL_VERSION},
46};
47
48// Validate features.
49#[cfg(all(target_env = "sgx", feature = "debug-mock-sgx"))]
50compile_error!("the debug-mock-sgx feature can only be enabled on non-sgx targets");
51
52#[cfg(all(target_env = "sgx", feature = "tdx"))]
53compile_error!("the tdx feature can only be enabled on non-sgx targets");
54
55#[cfg(all(feature = "tdx", feature = "debug-mock-sgx"))]
56compile_error!("the tdx feature can't be enabled together with debug-mock-sgx");
57
58lazy_static! {
59    pub static ref BUILD_INFO: BuildInfo = {
60        // Determine TEE type.
61        let tee_type = if cfg!(any(target_env = "sgx", feature = "debug-mock-sgx")) {
62            TeeType::Sgx
63        } else if cfg!(feature = "tdx") {
64            TeeType::Tdx
65        } else {
66            TeeType::None
67        };
68
69        // Determine build security.
70        #[allow(clippy::let_and_return)]
71        let is_secure = match tee_type {
72            TeeType::Sgx => {
73                // SGX build security depends on how it was built.
74                //
75                // Optimistically start out as "it could be secure", and any single insecure build time
76                // option will propagate failure.
77                let maybe_secure = true;
78
79                // Quote signature verification MUST be enabled.
80                let maybe_secure = maybe_secure && option_env!("OASIS_UNSAFE_SKIP_AVR_VERIFY").is_none();
81
82                // Disallow debug enclaves MUST be enabled.
83                let maybe_secure = maybe_secure && option_env!("OASIS_UNSAFE_ALLOW_DEBUG_ENCLAVES").is_none();
84
85                // Attestation `OutOfDate` and `ConfigurationNeeded` responses MUST count as attestation
86                // failure.
87                //
88                // Rationale: This is how remote attestation signifies that the host environment is
89                // insecure (eg: SMT is enabled when it should not be).
90                let maybe_secure = maybe_secure && option_env!("OASIS_UNSAFE_LAX_AVR_VERIFY").is_none();
91
92                // The enclave MUST NOT be a debug one.
93                #[cfg(target_env = "sgx")]
94                let maybe_secure = maybe_secure && !Report::for_self().attributes.flags.contains(AttributesFlags::DEBUG);
95
96                // The enclave MUST NOT be signed by a test key,
97                let enclave_identity = EnclaveIdentity::current().unwrap();
98                let fortanix_mrsigner = MrSigner::from("9affcfae47b848ec2caf1c49b4b283531e1cc425f93582b36806e52a43d78d1a");
99                let maybe_secure = maybe_secure && (enclave_identity.mr_signer != fortanix_mrsigner);
100
101                maybe_secure
102            }
103            TeeType::Tdx => {
104                // TDX build security depends on how it was built.
105                //
106                // Optimistically start out as "it could be secure", and any single insecure build time
107                // option will propagate failure.
108                let maybe_secure = true;
109
110                // Quote signature verification MUST be enabled.
111                let maybe_secure = maybe_secure && option_env!("OASIS_UNSAFE_SKIP_AVR_VERIFY").is_none();
112
113                // Disallow debug enclaves MUST be enabled.
114                let maybe_secure = maybe_secure && option_env!("OASIS_UNSAFE_ALLOW_DEBUG_ENCLAVES").is_none();
115
116                // Attestation `OutOfDate` and `ConfigurationNeeded` responses MUST count as attestation
117                // failure.
118                //
119                // Rationale: This is how remote attestation signifies that the host environment is
120                // insecure (eg: SMT is enabled when it should not be).
121                let maybe_secure = maybe_secure && option_env!("OASIS_UNSAFE_LAX_AVR_VERIFY").is_none();
122
123                // TODO: Debug TD attributes.
124
125                maybe_secure
126            }
127            TeeType::None => {
128                // Non-TEE builds are insecure by definition.
129                false
130            }
131        };
132
133        BuildInfo {
134            tee_type,
135            protocol_version: PROTOCOL_VERSION,
136            is_secure,
137        }
138    };
139}
140
141/// TEE type this build is for.
142#[derive(Debug, Default, PartialEq, Eq)]
143pub enum TeeType {
144    #[default]
145    None,
146    Sgx,
147    Tdx,
148}
149
150/// Runtime build information.
151#[derive(Debug)]
152pub struct BuildInfo {
153    /// TEE type this build is for.
154    pub tee_type: TeeType,
155    /// Supported runtime protocol version.
156    pub protocol_version: Version,
157    /// True iff the build can provide integrity and confidentiality.
158    pub is_secure: bool,
159}
160
161// Re-exports.
162pub use self::{
163    enclave_rpc::{demux::Demux as RpcDemux, dispatcher::Dispatcher as RpcDispatcher},
164    init::start_runtime,
165    protocol::Protocol,
166    transaction::dispatcher::Dispatcher as TxnDispatcher,
167};
168
169// Re-export the cbor crate.
170pub use cbor;