Skip to content

Commit

Permalink
Add Serialize/Deserialize to TargetInfo
Browse files Browse the repository at this point in the history
The `TargetInfo` type is used to go to/from an enclave from the
untrusted application, so it needs to support serialization.
  • Loading branch information
nick-mobilecoin committed Jul 18, 2023
1 parent e548815 commit ba4f5e8
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 6 deletions.
93 changes: 93 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion core/sys/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ rust-version = "1.62.1"
doctest = false

[dependencies]
serde = { version = "1.0.152", default-features = false, features = ["derive"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_with = { version = "3.1", default-features = false, features = ["macros"] }

[build-dependencies]
bindgen = "0.66.1"
Expand Down
4 changes: 1 addition & 3 deletions core/sys/types/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const CORE_TYPES: &[&str] = &[
"_sgx_report_data_t",
"_sgx_misc_attribute_t",
"_status_t",
"_target_info_t",
"sgx_config_id_t",
"sgx_config_svn_t",
"sgx_isv_svn_t",
Expand Down Expand Up @@ -105,7 +104,6 @@ fn main() {
"sgx_att_key_id_ext_t",
"sgx_qe_report_info_t",
"sgx_quote_nonce_t",
"sgx_target_info_t",
"sgx_report_t",
"sgx_report_body_t",
"sgx_key_id_t",
Expand All @@ -115,7 +113,7 @@ fn main() {
"sgx_attributes_t",
])
.dynamically_sized_types(["sgx_quote_t"])
.serialize_types(["sgx_measurement_t"])
.serialize_types(["sgx_measurement_t", "sgx_attributes_t"])
.derive_default([
"sgx_report_t",
"sgx_attributes_t",
Expand Down
21 changes: 21 additions & 0 deletions core/sys/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)]

use serde::{Deserialize, Serialize};
use serde_with::{serde_as, Bytes};

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

Expand Down Expand Up @@ -88,6 +89,26 @@ impl Default for sgx_report_body_t {
}
}

// Manually creating the bindings for `sgx_target_info_t` because of the need to derive serde for
// the `config_id` and `reserved3` fields. This structure is unlikely to change in size as the
// `reserved3` field is 384 bytes, appearing to be padding to make this structure 512 bytes. It is
// likely that any new fields will be taken from the `reserved3` space.
#[serde_as]
#[repr(C)]
#[derive(Eq, Hash, PartialEq, Clone, Debug, Copy, Serialize, Deserialize)]
pub struct sgx_target_info_t {
pub mr_enclave: sgx_measurement_t,
pub attributes: sgx_attributes_t,
pub reserved1: [u8; SGX_TARGET_INFO_RESERVED1_BYTES],
pub config_svn: sgx_config_svn_t,
pub misc_select: sgx_misc_select_t,
pub reserved2: [u8; SGX_TARGET_INFO_RESERVED2_BYTES],
#[serde_as(as = "Bytes")]
pub config_id: sgx_config_id_t,
#[serde_as(as = "Bytes")]
pub reserved3: [u8; SGX_TARGET_INFO_RESERVED3_BYTES],
}

impl Default for sgx_target_info_t {
fn default() -> Self {
Self {
Expand Down
1 change: 1 addition & 0 deletions core/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ getrandom = { version = "0.2", default-features = false, features = ["custom"] }
rand = "0.8.5"
textwrap = "0.16.0"
yare = "1.0.1"
serde_cbor = "0.11.1"
45 changes: 44 additions & 1 deletion core/types/src/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use crate::{
config_id::ConfigId, impl_newtype, Attributes, ConfigSvn, MiscellaneousSelect, MrEnclave,
};
use mc_sgx_core_sys_types::sgx_target_info_t;
use serde::{Deserialize, Serialize};

/// The target info
#[repr(transparent)]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq)]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct TargetInfo(sgx_target_info_t);

impl TargetInfo {
Expand Down Expand Up @@ -90,4 +91,46 @@ mod test {
assert_eq!(info.miscellaneous_select(), MiscellaneousSelect::from(6));
assert_eq!(info.config_id(), ConfigId::from([8; SGX_CONFIGID_SIZE]));
}

#[test]
fn serialize_from_target_info_t() {
let info = sgx_target_info_t {
mr_enclave: MrEnclave::from([3u8; MrEnclave::SIZE]).into(),
attributes: Attributes::default()
.set_flags(AttributeFlags::MODE_64BIT)
.set_extended_features_mask(ExtendedFeatureRequestMask::AVX)
.into(),
reserved1: [5u8; SGX_TARGET_INFO_RESERVED1_BYTES],
config_svn: 6,
misc_select: 7,
reserved2: [8u8; SGX_TARGET_INFO_RESERVED2_BYTES],
config_id: [9u8; SGX_CONFIGID_SIZE],
reserved3: [10u8; SGX_TARGET_INFO_RESERVED3_BYTES],
};

// cbor is the format to go to/from an enclave in the main MobileCoin repo
let bytes = serde_cbor::to_vec(&info).expect("failed to serialize");
let target_info: TargetInfo =
serde_cbor::from_slice(bytes.as_slice()).expect("failed to deserialize");

assert_eq!(
target_info.mr_enclave(),
MrEnclave::from([3u8; SGX_HASH_SIZE])
);
assert_eq!(
target_info.attributes(),
Attributes::default()
.set_flags(AttributeFlags::MODE_64BIT)
.set_extended_features_mask(ExtendedFeatureRequestMask::AVX)
);
assert_eq!(target_info.config_svn(), ConfigSvn::from(6));
assert_eq!(
target_info.miscellaneous_select(),
MiscellaneousSelect::from(7)
);
assert_eq!(
target_info.config_id(),
ConfigId::from([9; SGX_CONFIGID_SIZE])
);
}
}
6 changes: 5 additions & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ multiple-versions = "warn"
# Lint level for when a crate version requirement is `*`
wildcards = "deny"
highlight = "all"
allow = []
allow = [
# It's only used in dev and it's to maintain parity with the serialization in
# <https://github.com/mobilecoinfoundation/mobilecoin>
{ name = "serde_cbor"}
]
deny = [
# https://github.com/briansmith/ring/issues/774
{ name = "ring" },
Expand Down

0 comments on commit ba4f5e8

Please sign in to comment.