6#include "ccf/version.h"
14# include <openenclave/bits/result.h>
15# include <openenclave/host.h>
16# include <openenclave/trace.h>
19#if defined(PLATFORM_VIRTUAL) || defined(PLATFORM_SNP)
28 const std::string& file,
29 char const* expected_suffix,
32 if (!file.ends_with(expected_suffix))
37 for (
const char* suffix :
38 {
".signed",
".debuggable",
".so",
".enclave",
".virtual",
".snp"})
40 if (basename.ends_with(suffix))
42 basename = basename.substr(0, basename.size() - strlen(suffix));
45 const auto suggested = fmt::format(
"{}{}", basename, expected_suffix);
46 throw std::logic_error(fmt::format(
47 "Given enclave file '{}' does not have suffix expected for enclave "
49 "{}. Did you mean '{}'?",
51 nlohmann::json(type).dump(),
56 static std::pair<uint8_t*, size_t> allocate_8_aligned(
size_t size)
58 const auto aligned_size = (size + 7) & ~(7ull);
59 auto data =
static_cast<uint8_t*
>(std::aligned_alloc(8u, aligned_size));
62 throw std::runtime_error(fmt::format(
63 "Unable to allocate {} bytes for aligned data", aligned_size));
65 return std::make_pair(data, aligned_size);
79#if defined(PLATFORM_VIRTUAL) || defined(PLATFORM_SNP)
80 void* virtual_handle =
nullptr;
94 if (!std::filesystem::exists(path))
96 throw std::logic_error(
97 fmt::format(
"No enclave file found at {}", path));
105 uint32_t oe_flags = 0;
109 oe_flags |= OE_ENCLAVE_FLAG_DEBUG;
116 auto err = oe_create_ccf_enclave(
126 throw std::logic_error(
127 fmt::format(
"Could not create enclave: {}",
oe_result_str(err)));
130 throw std::logic_error(fmt::format(
131 "SGX enclaves are not supported in current build - cannot launch "
140#if defined(PLATFORM_SNP)
144 throw std::logic_error(fmt::format(
145 "SNP enclaves are not supported in current build - cannot launch "
154#if defined(PLATFORM_VIRTUAL)
158 throw std::logic_error(fmt::format(
159 "Virtual enclaves are not supported in current build - cannot "
168 throw std::logic_error(fmt::format(
169 "Unsupported enclave type: {}", nlohmann::json(type).dump()));
177 if (sgx_handle !=
nullptr)
179 auto err = oe_terminate_enclave(sgx_handle);
189#if defined(PLATFORM_SNP) || defined(PLATFORM_VIRTUAL)
190 if (virtual_handle !=
nullptr)
200 std::vector<uint8_t>&& startup_snapshot,
201 std::vector<uint8_t>& node_cert,
202 std::vector<uint8_t>& service_cert,
205 size_t num_worker_thread,
209 constexpr size_t enclave_version_size = 256;
210 std::vector<uint8_t> enclave_version_buf(enclave_version_size);
212 size_t node_cert_len = 0;
213 size_t service_cert_len = 0;
214 size_t enclave_version_len = 0;
218 auto config_s = nlohmann::json(ccf_config).dump();
219 auto [config, config_aligned_size] = allocate_8_aligned(config_s.size());
221 "Padding config of size {} to {} bytes",
223 config_aligned_size);
224 auto copy_end = std::copy(config_s.begin(), config_s.end(), config);
225 std::fill(copy_end, config + config_aligned_size, 0);
227 auto [snapshot, snapshot_aligned_size] =
228 allocate_8_aligned(startup_snapshot.size());
230 "Padding startup snapshot of size {} to {} bytes",
231 startup_snapshot.size(),
232 snapshot_aligned_size);
234 auto snapshot_copy_end =
235 std::copy(startup_snapshot.begin(), startup_snapshot.end(), snapshot);
236 std::fill(snapshot_copy_end, snapshot + snapshot_aligned_size, 0);
238#define CREATE_NODE_ARGS \
239 &status, (void*)&enclave_config, config, config_aligned_size, snapshot, \
240 snapshot_aligned_size, node_cert.data(), node_cert.size(), &node_cert_len, \
241 service_cert.data(), service_cert.size(), &service_cert_len, \
242 enclave_version_buf.data(), enclave_version_buf.size(), \
243 &enclave_version_len, start_type, enclave_log_level, num_worker_thread, \
250#if defined(PLATFORM_VIRTUAL) || defined(PLATFORM_SNP)
251 if (virtual_handle !=
nullptr)
257 if (sgx_handle !=
nullptr)
275 auto enclave_version = std::string(
276 enclave_version_buf.begin(),
277 enclave_version_buf.begin() + enclave_version_len);
278 if (ccf::ccf_version != enclave_version)
281 "Host/Enclave versions mismatch: {} != {}",
287 node_cert.resize(node_cert_len);
288 service_cert.resize(service_cert_len);
300#if defined(PLATFORM_VIRTUAL) || defined(PLATFORM_SNP)
301 if (virtual_handle !=
nullptr)
307 if (sgx_handle !=
nullptr)
315 throw std::logic_error(
316 fmt::format(
"Failed to call in enclave_run: {}",
oe_result_str(err)));
Enclave(const std::string &path, EnclaveType type, EnclavePlatform platform)
Definition enclave.h:92
CreateNodeStatus create_node(const EnclaveConfig &enclave_config, const StartupConfig &ccf_config, std::vector< uint8_t > &&startup_snapshot, std::vector< uint8_t > &node_cert, std::vector< uint8_t > &service_cert, StartType start_type, LoggerLevel enclave_log_level, size_t num_worker_thread, void *time_location)
Definition enclave.h:197
bool run()
Definition enclave.h:295
~Enclave()
Definition enclave.h:174
bool enclave_run()
Definition main.cpp:338
CreateNodeStatus enclave_create_node(void *enclave_config, uint8_t *ccf_config, size_t ccf_config_size, uint8_t *startup_snapshot_data, size_t startup_snapshot_size, uint8_t *node_cert, size_t node_cert_size, size_t *node_cert_len, uint8_t *service_cert, size_t service_cert_size, size_t *service_cert_len, uint8_t *enclave_version, size_t enclave_version_size, size_t *enclave_version_len, StartType start_type, LoggerLevel enclave_log_level, size_t num_worker_threads, void *time_location)
Definition main.cpp:59
CreateNodeStatus
Definition enclave_interface_types.h:8
@ OK
Definition enclave_interface_types.h:10
@ VersionMismatch
Definition enclave_interface_types.h:43
@ InternalError
Definition enclave_interface_types.h:13
StartType
Definition enclave_interface_types.h:113
#define LOG_DEBUG_FMT
Definition logger.h:380
#define LOG_FAIL_FMT
Definition logger.h:396
LoggerLevel
Definition logger_level.h:6
Definition configuration.h:13
EnclaveType
Definition configuration.h:15
EnclavePlatform
Definition configuration.h:27
void expect_enclave_file_suffix(const std::string &file, char const *expected_suffix, host::EnclaveType type)
Definition enclave.h:27
Definition configuration.h:41
Definition startup_config.h:79
void terminate_virtual_enclave(void *handle)
Definition virtual_enclave.h:91
void * load_virtual_enclave(const char *path)
Definition virtual_enclave.h:70
void oe_enclave_t
Definition virtual_enclave.h:44
oe_result_t virtual_run(void *virtual_enclave_handle, bool *_retval)
Definition virtual_enclave.h:179
@ OE_ENCLAVE_TYPE_SGX
Definition virtual_enclave.h:49
oe_result_t virtual_create_node(void *virtual_enclave_handle, CreateNodeStatus *status, void *enclave_config, uint8_t *ccf_config, size_t ccf_config_size, uint8_t *startup_snapshot, size_t startup_snapshot_size, uint8_t *node_cert, size_t node_cert_size, size_t *node_cert_len, uint8_t *service_cert, size_t service_cert_size, size_t *service_cert_len, uint8_t *enclave_version, size_t enclave_version_size, size_t *enclave_version_len, StartType start_type, LoggerLevel enclave_log_level, size_t num_worker_thread, void *time_location)
Definition virtual_enclave.h:100
#define oe_result_str(x)
Definition virtual_enclave.h:52
constexpr oe_result_t OE_OK
Definition virtual_enclave.h:41
int oe_result_t
Definition virtual_enclave.h:40
constexpr oe_result_t OE_FAILURE
Definition virtual_enclave.h:42