Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/edlcodegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ edlcodegen-enclave = { version = "0.1.0", path = "crates/libs/edlcodegen-enclave
edlcodegen-macros = { version = "0.1.0", path = "crates/libs/edlcodegen-macros" }
edlcodegen-tools = { version = "0.1.0", path = "crates/libs/edlcodegen-tools" }
spin = "0.10.0"
widestring = { version = "1.2.1", default-features = false, features = ["alloc"] }
windows = "0.62.2"
windows-link = "0.2.1"
windows-result = {version = "0.4.1", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions rust/edlcodegen/crates/libs/edlcodegen-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ edlcodegen-tools = { workspace = true }
[dependencies]
edlcodegen-macros = { workspace = true }
flatbuffers = { workspace = true }
widestring = { workspace = true }
6 changes: 6 additions & 0 deletions rust/edlcodegen/crates/libs/edlcodegen-core/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,9 @@ macro_rules! return_hr_as_pvoid {
return $crate::edl_core_types::S_OK as *mut core::ffi::c_void;
}};
}

pub fn assign_if_some<T>(dst: Option<&mut T>, src: Option<T>) {
if let (Some(dst_obj), Some(src_obj)) = (dst, src) {
*dst_obj = src_obj;
}
}
1 change: 1 addition & 0 deletions rust/edlcodegen/crates/libs/edlcodegen-enclave/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ pub mod win_enclave_bindings;
pub use edlcodegen_core::EdlDerive;
pub use edlcodegen_core::edl_core_types::AbiError;
pub use edlcodegen_core::flatbuffer_support::FlatbufferPack;
pub use edlcodegen_core::helpers::assign_if_some;
pub use edlcodegen_core::impl_flatbuffer_pack;
pub use edlcodegen_core::return_hr_as_pvoid;
2 changes: 1 addition & 1 deletion rust/edlcodegen/crates/libs/edlcodegen-host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use edlcodegen_core::edl_core_types::AbiError;
pub use edlcodegen_core::edl_core_types::EnclaveHandle;
pub use edlcodegen_core::flatbuffer_support::FlatbufferPack;
pub use edlcodegen_core::helpers::{
abi_func_to_address, allocate_memory_ffi, deallocate_memory_ffi,
abi_func_to_address, allocate_memory_ffi, assign_if_some, deallocate_memory_ffi,
};
pub use edlcodegen_core::impl_flatbuffer_pack;
pub use edlcodegen_core::return_hr_as_pvoid;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use test_enclave_gen::implementation::trusted::Trusted;
use test_enclave_gen::AbiError;
use test_enclave_gen::stubs::untrusted::print;
use alloc::string::ToString;
use tracelogging as tlg;
use crate::{HELLO_WORLD_PROVIDER};

Expand All @@ -20,7 +19,7 @@ impl Trusted for EnclaveImpl {
);

// Print message to console from within the enclave.
print(&"Performing secret math operation inside VTL1...".to_string())?;
print("Performing secret math operation inside VTL1...")?;
Ok(val1 + val2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use test_host_gen::AbiError;
pub struct HostImpl{}

impl Untrusted for HostImpl {
fn print(data: &String) -> Result<(), AbiError> {
fn print(data: &str) -> Result<(), AbiError> {
println!("{}", data);
Ok(())
}
Expand Down
3 changes: 1 addition & 2 deletions rust/sdk/crates/libs/sdk-enclave/src/common/sdk_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use crate::etw;
use crate::userboundkey;
use alloc::vec::Vec;
use sdk_enclave_gen::AbiError;
use sdk_enclave_gen::implementation::trusted::Trusted;
use sdk_enclave_gen::implementation::types::attestationReportAndSessionInfo;
Expand All @@ -25,7 +24,7 @@ pub struct EnclaveImpl;
#[allow(non_snake_case)]
impl Trusted for EnclaveImpl {
fn userboundkey_get_attestation_report(
challenge: &Vec<u8>,
challenge: &[u8],
) -> Result<attestationReportAndSessionInfo, AbiError> {
userboundkey::userboundkey_get_attestation_report(challenge)
}
Expand Down
7 changes: 3 additions & 4 deletions rust/sdk/crates/libs/sdk-enclave/src/etw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ unsafe extern "system" fn EtwSetInformation(
let info_slice =
unsafe { core::slice::from_raw_parts(information, information_length as usize) };

let result =
untrusted::event_set_information(reg_handle, information_class, &info_slice.to_vec());
let result = untrusted::event_set_information(reg_handle, information_class, info_slice);

result_from_abi(result)
}
Expand Down Expand Up @@ -284,8 +283,8 @@ unsafe extern "system" fn EtwWriteTransfer(
let result = untrusted::event_write_transfer(
reg_handle,
&abi_descriptor,
&activity_guid,
&related_guid,
activity_guid.as_ref(),
related_guid.as_ref(),
&abi_data.to_vec(),
);

Expand Down
63 changes: 19 additions & 44 deletions rust/sdk/crates/libs/sdk-enclave/src/userboundkey/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,32 +277,22 @@ pub fn create_user_bound_key_with_custom_key(
custom_key_bytes: &[u8],
) -> Result<Vec<u8>, UserBoundKeyError> {
// Format the key name (calls VTL0) - this is the FIRST VTL0 call
// Create WString from key_name slice
let key_name_wstring = sdk_enclave_gen::implementation::types::edl::WString {
wchars: key_name.as_slice().to_vec(),
};
let formatted_key_name_wstring = untrusted::userboundkey_format_key_name(&key_name_wstring)
.map_err(UserBoundKeyError::AbiError)?;
let formatted_key_name_wstring =
untrusted::userboundkey_format_key_name(key_name).map_err(UserBoundKeyError::AbiError)?;

// Security validation (wide string version)
let formatted_key_name_u16str = U16Str::from_slice(&formatted_key_name_wstring.wchars);
validate_formatted_key_name_wide(formatted_key_name_u16str, key_name)?;
validate_formatted_key_name_wide(&formatted_key_name_wstring, key_name)?;

// Get enclave base address
let enclave_ptr = get_enclave_base_address_u64()?;

// Create WString for message
let message_wstring = sdk_enclave_gen::implementation::types::edl::WString {
wchars: message.as_slice().to_vec(),
};

// Establish session (calls VTL0 which triggers Windows Hello)
// Requires at least 2 enclave threads - one for this call and one for the callback
let credential_and_session = untrusted::userboundkey_establish_session_for_create(
enclave_ptr,
&key_name_wstring,
key_name,
BCRYPT_ECDH_P384_ALG_HANDLE_U64,
&message_wstring,
message,
window_id,
cache_config,
key_credential_creation_option,
Expand All @@ -320,13 +310,10 @@ pub fn create_user_bound_key_with_custom_key(
let mut encrypted_request_size: u32 = 0;
let mut local_nonce: u64 = 0;

// Create null-terminated wide string for Windows API
let formatted_key_name_with_null = U16String::from(formatted_key_name_u16str);

unsafe {
let hr = CreateUserBoundKeyRequestForRetrieveAuthorizationContext(
session_handle.get(),
formatted_key_name_with_null.as_ptr(),
formatted_key_name_wstring.as_ptr(),
&mut local_nonce,
&mut encrypted_request_ptr,
&mut encrypted_request_size,
Expand All @@ -350,7 +337,7 @@ pub fn create_user_bound_key_with_custom_key(
let auth_context_blob = untrusted::userboundkey_get_authorization_context_from_credential(
credential.get(),
&encrypted_request,
&message_wstring,
message,
window_id,
)
.map_err(UserBoundKeyError::AbiError)?;
Expand Down Expand Up @@ -378,7 +365,7 @@ pub fn create_user_bound_key_with_custom_key(

unsafe {
let hr = ValidateUserBoundKeyAuthContext(
formatted_key_name_with_null.as_ptr(),
formatted_key_name_wstring.as_ptr(),
auth_context.get(),
1,
&prop,
Expand Down Expand Up @@ -451,29 +438,20 @@ pub fn load_user_bound_key(
let ephemeral_public_key = get_ephemeral_public_key_bytes_from_bound_key(&bound_key_bytes)?;

// Format the key name (calls VTL0)
let key_name_wstring = sdk_enclave_gen::implementation::types::edl::WString {
wchars: key_name.as_slice().to_vec(),
};
let formatted_key_name_wstring = untrusted::userboundkey_format_key_name(&key_name_wstring)
.map_err(UserBoundKeyError::AbiError)?;
let formatted_key_name_wstring =
untrusted::userboundkey_format_key_name(key_name).map_err(UserBoundKeyError::AbiError)?;

// Security validation (wide string version)
let formatted_key_name_u16str = U16Str::from_slice(&formatted_key_name_wstring.wchars);
validate_formatted_key_name_wide(formatted_key_name_u16str, key_name)?;
validate_formatted_key_name_wide(&formatted_key_name_wstring, key_name)?;

// Get enclave base address
let enclave_ptr = get_enclave_base_address_u64()?;

// Create WString for message
let message_wstring = sdk_enclave_gen::implementation::types::edl::WString {
wchars: message.as_slice().to_vec(),
};

// Establish session for load (calls VTL0)
let credential_and_session = untrusted::userboundkey_establish_session_for_load(
enclave_ptr,
&key_name_wstring,
&message_wstring,
key_name,
message,
window_id,
)
.map_err(UserBoundKeyError::AbiError)?;
Expand All @@ -489,13 +467,10 @@ pub fn load_user_bound_key(
let mut encrypted_rac_size: u32 = 0;
let mut local_nonce: u64 = 0;

// Create null-terminated wide string for Windows API
let formatted_key_name_with_null = U16String::from(formatted_key_name_u16str);

unsafe {
let hr = CreateUserBoundKeyRequestForRetrieveAuthorizationContext(
session_handle.get(),
formatted_key_name_with_null.as_ptr(),
formatted_key_name_wstring.as_ptr(),
&mut local_nonce,
&mut encrypted_rac_ptr,
&mut encrypted_rac_size,
Expand All @@ -518,7 +493,7 @@ pub fn load_user_bound_key(
let auth_context_blob = untrusted::userboundkey_get_authorization_context_from_credential(
credential.get(),
&encrypted_rac_request,
&message_wstring,
message,
window_id,
)
.map_err(UserBoundKeyError::AbiError)?;
Expand Down Expand Up @@ -546,7 +521,7 @@ pub fn load_user_bound_key(

unsafe {
let hr = ValidateUserBoundKeyAuthContext(
formatted_key_name_with_null.as_ptr(),
formatted_key_name_wstring.as_ptr(),
auth_context.get(),
1,
&prop,
Expand All @@ -561,7 +536,7 @@ pub fn load_user_bound_key(
unsafe {
let hr = CreateUserBoundKeyRequestForDeriveSharedSecret(
session_handle.get(),
formatted_key_name_with_null.as_ptr(),
formatted_key_name_wstring.as_ptr(),
ephemeral_public_key.as_ptr() as *const core::ffi::c_void,
ephemeral_public_key.len() as u32,
&mut local_nonce,
Expand All @@ -586,7 +561,7 @@ pub fn load_user_bound_key(
let secret = untrusted::userboundkey_get_secret_from_credential(
credential.get(),
&encrypted_dss_request,
&message_wstring,
message,
window_id,
)
.map_err(UserBoundKeyError::AbiError)?;
Expand Down Expand Up @@ -659,7 +634,7 @@ pub fn reseal_user_bound_key(
#[allow(non_snake_case)]
#[allow(clippy::ptr_arg)] // Signature must match EDL-generated trait
pub fn userboundkey_get_attestation_report(
challenge: &Vec<u8>,
challenge: &[u8],
) -> Result<attestationReportAndSessionInfo, AbiError> {
let mut report_ptr: *mut core::ffi::c_void = core::ptr::null_mut();
let mut report_size: u32 = 0;
Expand Down
2 changes: 1 addition & 1 deletion rust/sdk/crates/libs/sdk-host/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ pub mod sdk_impls;
pub use sdk_impls::HostImpl;

/// Print a message from the enclave to the host's standard output.
pub fn enclave_println(msg: &String) {
pub fn enclave_println(msg: &str) {
println!("[Enclave] {}", msg);
}
37 changes: 18 additions & 19 deletions rust/sdk/crates/libs/sdk-host/src/common/sdk_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
//! This pattern allows feature implementations to live in their own modules
//! while the trait impl acts as a thin wrapper.

use crate::etw;
use crate::userboundkey;
use sdk_host_gen::AbiError;
use sdk_host_gen::implementation::types::{
EventDataDescriptor, EventDescriptor, Guid, credentialAndSessionInfo, edl::WString,
keyCredentialCacheConfig,
EventDataDescriptor, EventDescriptor, Guid, credentialAndSessionInfo, keyCredentialCacheConfig,
};
use sdk_host_gen::implementation::untrusted::Untrusted;

use crate::etw;
use crate::userboundkey;
use widestring::{U16Str, U16String};

/// SDK host implementation of the Untrusted trait.
///
Expand All @@ -28,9 +27,9 @@ pub struct HostImpl;
impl Untrusted for HostImpl {
fn userboundkey_establish_session_for_create(
enclave: u64,
keyName: &WString,
keyName: &U16Str,
ecdhProtocol: u64,
message: &WString,
message: &U16Str,
windowId: u64,
cacheConfig: &keyCredentialCacheConfig,
keyCredentialCreationOption: u32,
Expand All @@ -48,17 +47,17 @@ impl Untrusted for HostImpl {

fn userboundkey_establish_session_for_load(
enclave: u64,
keyName: &WString,
message: &WString,
keyName: &U16Str,
message: &U16Str,
windowId: u64,
) -> Result<credentialAndSessionInfo, AbiError> {
userboundkey::userboundkey_establish_session_for_load(enclave, keyName, message, windowId)
}

fn userboundkey_get_authorization_context_from_credential(
credential: u64,
encryptedRequest: &Vec<u8>,
message: &WString,
encryptedRequest: &[u8],
message: &U16Str,
windowId: u64,
) -> Result<Vec<u8>, AbiError> {
userboundkey::userboundkey_get_authorization_context_from_credential(
Expand All @@ -71,8 +70,8 @@ impl Untrusted for HostImpl {

fn userboundkey_get_secret_from_credential(
credential: u64,
encryptedRequest: &Vec<u8>,
message: &WString,
encryptedRequest: &[u8],
message: &U16Str,
windowId: u64,
) -> Result<Vec<u8>, AbiError> {
userboundkey::userboundkey_get_secret_from_credential(
Expand All @@ -83,7 +82,7 @@ impl Untrusted for HostImpl {
)
}

fn userboundkey_format_key_name(keyName: &WString) -> Result<WString, AbiError> {
fn userboundkey_format_key_name(keyName: &U16Str) -> Result<U16String, AbiError> {
userboundkey::userboundkey_format_key_name(keyName)
}

Expand All @@ -107,17 +106,17 @@ impl Untrusted for HostImpl {
fn event_write_transfer(
reg_handle: u64,
descriptor: &EventDescriptor,
activity_id: &Option<Guid>,
related_id: &Option<Guid>,
user_data: &Vec<EventDataDescriptor>,
activity_id: Option<&Guid>,
related_id: Option<&Guid>,
user_data: &[EventDataDescriptor],
) -> Result<u32, AbiError> {
etw::event_write_transfer(reg_handle, descriptor, activity_id, related_id, user_data)
}

fn event_set_information(
reg_handle: u64,
information_class: u32,
information: &Vec<u8>,
information: &[u8],
) -> Result<u32, AbiError> {
etw::event_set_information(reg_handle, information_class, information)
}
Expand All @@ -129,7 +128,7 @@ impl Untrusted for HostImpl {
etw::event_activity_id_control(control_code, activity_id)
}

fn println(msg: &String) -> Result<(), AbiError> {
fn println(msg: &str) -> Result<(), AbiError> {
super::enclave_println(msg);
Ok(())
}
Expand Down
Loading