use crate::api::tee_api_mm::TEE_CheckMemoryAccessRights;
use crate::api::tee_api_panic::TEE_Panic;
use crate::syscalls::syscall_table::{
_utee_cryp_obj_alloc, _utee_cryp_obj_close, _utee_cryp_obj_copy, _utee_cryp_obj_generate_key,
_utee_cryp_obj_get_attr, _utee_cryp_obj_get_info, _utee_cryp_obj_populate,
_utee_cryp_obj_reset, _utee_cryp_obj_restrict_usage, _utee_storage_alloc_enum,
_utee_storage_free_enum, _utee_storage_next_enum, _utee_storage_obj_create,
_utee_storage_obj_del, _utee_storage_obj_open, _utee_storage_obj_read, _utee_storage_obj_seek,
_utee_storage_obj_trunc, _utee_storage_obj_write, _utee_storage_reset_enum,
_utee_storage_start_enum,
};
use crate::tee_api_defines::*;
use crate::tee_api_types::{
TEE_Attribute, TEE_ObjectEnumHandle, TEE_ObjectHandle, TEE_ObjectInfo, TEE_Result, TEE_Whence,
};
use crate::utee_types::utee_attribute;
pub const TEE_USAGE_DEFAULT: u32 = 0xffffffff;
pub unsafe fn __utee_from_attr(
ua: *mut utee_attribute,
attrs: *const TEE_Attribute,
attr_count: u32,
) {
unsafe {
for n in 0..attr_count as usize {
let ua_ptr = ua.add(n);
let attr_ptr = attrs.add(n);
(*ua_ptr).attribute_id = (*attr_ptr).attributeID;
if (*attr_ptr).attributeID & TEE_ATTR_FLAG_VALUE != 0 {
(*ua_ptr).a = (*attr_ptr).content.value.a as u64;
(*ua_ptr).b = (*attr_ptr).content.value.b as u64;
} else {
(*ua_ptr).a = (*attr_ptr).content.memref.buffer as u64;
(*ua_ptr).b = (*attr_ptr).content.memref.size as u64;
}
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetObjectInfo(object: TEE_ObjectHandle, object_info: &mut TEE_ObjectInfo) {
let mut info = unsafe { std::mem::zeroed() }; let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) };
if res != TEE_SUCCESS as usize {
TEE_Panic(res as u32);
}
if info.obj_type == TEE_TYPE_CORRUPTED_OBJECT {
object_info.objectSize = 0;
object_info.maxObjectSize = 0;
object_info.objectUsage = 0;
object_info.dataSize = 0;
object_info.dataPosition = 0;
object_info.handleFlags = 0;
} else {
object_info.objectType = info.obj_type;
object_info.objectSize = info.obj_size;
object_info.maxObjectSize = info.max_obj_size;
object_info.objectUsage = info.obj_usage;
object_info.dataSize = info.data_size as usize;
object_info.dataPosition = info.data_pos as usize;
object_info.handleFlags = info.handle_flags;
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetObjectInfo1(
object: TEE_ObjectHandle,
object_info: &mut TEE_ObjectInfo,
) -> TEE_Result {
let mut info = unsafe { std::mem::zeroed() };
let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
if res != TEE_SUCCESS
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res as u32);
}
object_info.objectType = info.obj_type;
object_info.objectSize = info.obj_size;
object_info.maxObjectSize = info.max_obj_size;
object_info.objectUsage = info.obj_usage;
object_info.dataSize = info.data_size as usize;
object_info.dataPosition = info.data_pos as usize;
object_info.handleFlags = info.handle_flags;
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_RestrictObjectUsage(object: TEE_ObjectHandle, object_usage: u32) {
let mut info = unsafe { std::mem::zeroed() };
unsafe {
_utee_cryp_obj_get_info(object as u64, &mut info);
}
if info.obj_type == TEE_TYPE_CORRUPTED_OBJECT {
return;
}
let res = TEE_RestrictObjectUsage1(object, object_usage);
if res != TEE_SUCCESS {
TEE_Panic(res as u32);
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_RestrictObjectUsage1(
object: TEE_ObjectHandle,
object_usage: u32,
) -> TEE_Result {
let res =
unsafe { _utee_cryp_obj_restrict_usage(object as u64, object_usage as u64) } as TEE_Result;
if res != TEE_SUCCESS
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetObjectBufferAttribute(
object: TEE_ObjectHandle,
attribute_id: u32,
buffer: *mut core::ffi::c_void,
size: *mut usize,
) -> TEE_Result {
if cfg!(feature = "strict_annotation_checks") {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
size as *mut core::ffi::c_void,
std::mem::size_of::<usize>(),
);
if res != 0 {
eprintln!("[inout] size: error {:#010x}", res);
TEE_Panic(0);
}
}
let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
if res != TEE_SUCCESS {
return check_result_and_panic(res);
}
if attribute_id & TEE_ATTR_FLAG_VALUE != 0 {
return check_result_and_panic(TEE_ERROR_BAD_PARAMETERS);
}
let mut buffer_size: u64 = 0;
unsafe {
if !size.is_null() {
buffer_size = *size as u64;
}
}
let res = unsafe {
_utee_cryp_obj_get_attr(object as u64, attribute_id as u64, buffer, &mut buffer_size)
} as TEE_Result;
unsafe {
if !size.is_null() {
*size = buffer_size as usize;
}
}
check_result_and_panic(res)
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetObjectBufferAttribute1(
object: TEE_ObjectHandle,
attribute_id: u32,
buffer: *mut core::ffi::c_void,
size: *mut usize,
) -> TEE_Result {
if size.is_null() {
return TEE_ERROR_BAD_PARAMETERS;
}
let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
if res != TEE_SUCCESS {
return check_result_and_panic(res);
}
if attribute_id & TEE_ATTR_FLAG_VALUE != 0 {
return TEE_ERROR_BAD_PARAMETERS;
}
let mut required_size: u64 = 0;
let res = unsafe {
_utee_cryp_obj_get_attr(
object as u64,
attribute_id as u64,
core::ptr::null_mut(),
&mut required_size,
) as TEE_Result
};
if res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER {
unsafe {
*size = required_size as usize;
}
} else {
return check_result_and_panic(res);
}
if !buffer.is_null() && unsafe { *size } >= required_size as usize {
let res = unsafe {
_utee_cryp_obj_get_attr(
object as u64,
attribute_id as u64,
buffer,
&mut required_size,
) as TEE_Result
};
if res != TEE_SUCCESS {
return check_result_and_panic(res);
}
unsafe {
*size = required_size as usize;
}
}
TEE_SUCCESS
}
fn check_result_and_panic(res: TEE_Result) -> TEE_Result {
if res != TEE_SUCCESS
&& res != TEE_ERROR_ITEM_NOT_FOUND
&& res != TEE_ERROR_SHORT_BUFFER
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res as u32);
}
res
}
fn handle_result_and_return(
res: TEE_Result,
a: *mut u32,
b: *mut u32,
value_a: u32,
value_b: u32,
) -> TEE_Result {
if res != TEE_SUCCESS
&& res != TEE_ERROR_ITEM_NOT_FOUND
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res);
}
if res == TEE_SUCCESS {
if !a.is_null() {
unsafe { *a = value_a };
}
if !b.is_null() {
unsafe { *b = value_b };
}
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetObjectValueAttribute(
object: TEE_ObjectHandle,
attribute_id: u32,
a: *mut u32,
b: *mut u32,
) -> TEE_Result {
if cfg!(feature = "strict_annotation_checks") {
if !a.is_null() {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
a as *mut core::ffi::c_void,
std::mem::size_of::<u32>(),
);
if res != 0 {
eprintln!("[inout] a: error {:#010x}", res);
TEE_Panic(0);
}
}
if !b.is_null() {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
b as *mut core::ffi::c_void,
std::mem::size_of::<u32>(),
);
if res != 0 {
eprintln!("[inout] b: error {:#010x}", res);
TEE_Panic(0);
}
}
}
let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
if res != TEE_SUCCESS {
return handle_result_and_return(res, a, b, 0, 0);
}
if attribute_id & TEE_ATTR_FLAG_VALUE == 0 {
let res = TEE_ERROR_BAD_PARAMETERS;
return handle_result_and_return(res, a, b, 0, 0);
}
let mut buf = [0u32; 2];
let mut size = std::mem::size_of_val(&buf) as u64;
let res = unsafe {
_utee_cryp_obj_get_attr(
object as u64,
attribute_id as u64,
buf.as_mut_ptr() as *mut core::ffi::c_void,
&mut size,
) as TEE_Result
};
if res != TEE_SUCCESS
&& res != TEE_ERROR_ITEM_NOT_FOUND
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res);
}
if size != std::mem::size_of_val(&buf) as u64 {
TEE_Panic(0);
}
if res == TEE_SUCCESS {
if !a.is_null() {
unsafe { *a = buf[0] };
}
if !b.is_null() {
unsafe { *b = buf[1] };
}
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_CloseObject(object: TEE_ObjectHandle) {
if object.is_null() {
return;
}
let res = unsafe { _utee_cryp_obj_close(object as u64) } as TEE_Result;
if res != TEE_SUCCESS {
TEE_Panic(res as u32);
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_AllocateTransientObject(
object_type: u32, max_object_size: u32,
object: *mut TEE_ObjectHandle,
) -> TEE_Result {
if object_type == TEE_TYPE_DATA {
return TEE_ERROR_NOT_SUPPORTED;
}
if cfg!(feature = "strict_annotation_checks") {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
object as *mut core::ffi::c_void,
std::mem::size_of::<TEE_ObjectHandle>(),
);
if res != 0 {
eprintln!("[inout] object: error {:#010x}", res);
TEE_Panic(0);
}
}
let mut obj: u32 = 0;
let res = unsafe { _utee_cryp_obj_alloc(object_type as u64, max_object_size as u64, &mut obj) }
as TEE_Result;
if res != TEE_SUCCESS && res != TEE_ERROR_OUT_OF_MEMORY && res != TEE_ERROR_NOT_SUPPORTED {
TEE_Panic(res as u32);
}
if res == TEE_SUCCESS {
unsafe {
*object = obj as TEE_ObjectHandle;
}
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_FreeTransientObject(object: TEE_ObjectHandle) {
if object.is_null() {
return;
}
let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
if res != TEE_SUCCESS {
TEE_Panic(res as u32);
}
if (info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0 {
TEE_Panic(0);
}
let res = unsafe { _utee_cryp_obj_close(object as u64) } as TEE_Result;
if res != TEE_SUCCESS {
TEE_Panic(res as u32);
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_ResetTransientObject(object: TEE_ObjectHandle) {
if object.is_null() {
return;
}
let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
if res != TEE_SUCCESS {
TEE_Panic(res as u32);
}
if (info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0 {
TEE_Panic(0);
}
let res = unsafe { _utee_cryp_obj_reset(object as u64) } as TEE_Result;
if res != TEE_SUCCESS {
TEE_Panic(res as u32);
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_PopulateTransientObject(
object: TEE_ObjectHandle,
attrs: *const TEE_Attribute,
attr_count: u32,
) -> TEE_Result {
let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
if res != TEE_SUCCESS {
TEE_Panic(res as u32);
}
if (info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0 {
TEE_Panic(0);
}
if (info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0 {
TEE_Panic(0);
}
let mut ua = vec![utee_attribute::default(); attr_count as usize];
unsafe {
__utee_from_attr(ua.as_mut_ptr(), attrs, attr_count);
}
let res = unsafe {
_utee_cryp_obj_populate(object as u64, ua.as_mut_ptr(), attr_count as u64) as TEE_Result
};
if res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS {
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_InitRefAttribute(
attr: *mut TEE_Attribute,
attribute_id: u32,
buffer: *const core::ffi::c_void,
length: usize,
) {
if cfg!(feature = "strict_annotation_checks") {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_WRITE,
attr as *mut core::ffi::c_void,
std::mem::size_of::<TEE_Attribute>(),
);
if res != 0 {
eprintln!("[out] attr: error {:#010x}", res);
TEE_Panic(0);
}
}
if (attribute_id & TEE_ATTR_FLAG_VALUE) != 0 {
TEE_Panic(0);
}
unsafe {
if !attr.is_null() {
(*attr).attributeID = attribute_id;
(*attr).content.memref.buffer = buffer as *mut core::ffi::c_void;
(*attr).content.memref.size = length;
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_InitValueAttribute(
attr: *mut TEE_Attribute,
attribute_id: u32,
a: u32,
b: u32,
) {
if cfg!(feature = "strict_annotation_checks") {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_WRITE,
attr as *mut core::ffi::c_void,
std::mem::size_of::<TEE_Attribute>(),
);
if res != 0 {
eprintln!("[out] attr: error {:#010x}", res);
TEE_Panic(0);
}
}
if (attribute_id & TEE_ATTR_FLAG_VALUE) == 0 {
TEE_Panic(0);
}
unsafe {
if !attr.is_null() {
(*attr).attributeID = attribute_id;
(*attr).content.value.a = a;
(*attr).content.value.b = b;
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_CopyObjectAttributes(
dest_object: TEE_ObjectHandle,
src_object: TEE_ObjectHandle,
) {
let mut src_info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
let _res = unsafe { _utee_cryp_obj_get_info(src_object as u64, &mut src_info) } as TEE_Result;
if src_info.obj_type == TEE_TYPE_CORRUPTED_OBJECT {
return;
}
let res = TEE_CopyObjectAttributes1(dest_object, src_object);
if res != TEE_SUCCESS {
TEE_Panic(res as u32);
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_CopyObjectAttributes1(
dest_object: TEE_ObjectHandle,
src_object: TEE_ObjectHandle,
) -> TEE_Result {
let mut dst_info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
let mut src_info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
let mut res =
unsafe { _utee_cryp_obj_get_info(dest_object as u64, &mut dst_info) } as TEE_Result;
if res != TEE_SUCCESS {
return check_copy_object_attributes_result(res);
}
res = unsafe { _utee_cryp_obj_get_info(src_object as u64, &mut src_info) } as TEE_Result;
if res != TEE_SUCCESS {
return check_copy_object_attributes_result(res);
}
if (src_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
TEE_Panic(0);
}
if (dst_info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0 {
TEE_Panic(0);
}
if (dst_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0 {
TEE_Panic(0);
}
res = unsafe { _utee_cryp_obj_copy(dest_object as u64, src_object as u64) } as TEE_Result;
check_copy_object_attributes_result(res)
}
fn check_copy_object_attributes_result(res: TEE_Result) -> TEE_Result {
if res != TEE_SUCCESS
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_GenerateKey(
object: TEE_ObjectHandle,
key_size: u32,
params: *const TEE_Attribute,
param_count: u32,
) -> TEE_Result {
if cfg!(feature = "strict_annotation_checks") && param_count > 0 {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_READ,
params as *mut core::ffi::c_void,
std::mem::size_of::<TEE_Attribute>() * param_count as usize,
);
if res != 0 {
eprintln!("[in] attrs: error {:#010x}", res);
TEE_Panic(0);
}
}
let mut ua = vec![utee_attribute::default(); param_count as usize];
unsafe {
__utee_from_attr(ua.as_mut_ptr(), params, param_count);
}
let res = unsafe {
_utee_cryp_obj_generate_key(
object as u64,
key_size as u64,
ua.as_ptr(),
param_count as u64,
)
} as TEE_Result;
if res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS {
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_OpenPersistentObject(
storage_id: u32,
object_id: *const core::ffi::c_void,
object_id_len: usize,
flags: u32,
object: *mut TEE_ObjectHandle,
) -> TEE_Result {
if cfg!(feature = "strict_annotation_checks") {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_WRITE,
object as *mut core::ffi::c_void,
std::mem::size_of::<TEE_ObjectHandle>(),
);
if res != 0 {
eprintln!("[out] object: error {:#010x}", res);
TEE_Panic(0);
}
}
let mut obj: u32 = 0;
let res = unsafe {
_utee_storage_obj_open(
storage_id as u64,
object_id,
object_id_len,
flags as u64,
&mut obj,
)
} as TEE_Result;
if res == TEE_SUCCESS {
unsafe {
*object = obj as TEE_ObjectHandle;
}
} else {
unsafe {
*object = core::ptr::null_mut();
}
}
if res != TEE_SUCCESS
&& res != TEE_ERROR_ITEM_NOT_FOUND
&& res != TEE_ERROR_ACCESS_CONFLICT
&& res != TEE_ERROR_OUT_OF_MEMORY
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_CreatePersistentObject(
storage_id: u32,
object_id: *const core::ffi::c_void,
object_id_len: usize,
flags: u32,
attributes: TEE_ObjectHandle,
initial_data: *const core::ffi::c_void,
initial_data_len: usize,
object: *mut TEE_ObjectHandle,
) -> TEE_Result {
let mut obj: u32 = 0;
let obj_ptr: *mut u32;
if !object.is_null() {
if cfg!(feature = "strict_annotation_checks") {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_WRITE,
object as *mut core::ffi::c_void,
std::mem::size_of::<TEE_ObjectHandle>(),
);
if res != 0 {
eprintln!("[out] object: error {:#010x}", res);
TEE_Panic(0);
}
}
obj_ptr = &mut obj;
} else {
obj_ptr = core::ptr::null_mut();
}
let res = unsafe {
_utee_storage_obj_create(
storage_id as u64,
object_id,
object_id_len,
flags as u64,
attributes as u64,
initial_data,
initial_data_len,
obj_ptr,
)
} as TEE_Result;
if res == TEE_SUCCESS && !object.is_null() {
unsafe {
*object = obj as TEE_ObjectHandle;
}
} else if res != TEE_SUCCESS && !object.is_null() {
unsafe {
*object = core::ptr::null_mut();
}
}
if res != TEE_SUCCESS
&& res != TEE_ERROR_ITEM_NOT_FOUND
&& res != TEE_ERROR_ACCESS_CONFLICT
&& res != TEE_ERROR_OUT_OF_MEMORY
&& res != TEE_ERROR_STORAGE_NO_SPACE
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_CloseAndDeletePersistentObject(object: TEE_ObjectHandle) {
if object.is_null() {
return;
}
let res = TEE_CloseAndDeletePersistentObject1(object);
if res != TEE_SUCCESS {
TEE_Panic(0);
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_CloseAndDeletePersistentObject1(object: TEE_ObjectHandle) -> TEE_Result {
if object.is_null() {
return TEE_SUCCESS;
}
let res = unsafe { _utee_storage_obj_del(object as u64) } as TEE_Result;
if res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE {
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_RenamePersistentObject(
object: TEE_ObjectHandle,
_new_object_id: *const core::ffi::c_void,
_new_object_id_len: usize,
) -> TEE_Result {
let res = if object.is_null() {
TEE_ERROR_ITEM_NOT_FOUND
} else {
TEE_ERROR_ITEM_NOT_FOUND
};
if res != TEE_SUCCESS
&& res != TEE_ERROR_ACCESS_CONFLICT
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_AllocatePersistentObjectEnumerator(
object_enumerator: *mut TEE_ObjectEnumHandle,
) -> TEE_Result {
if cfg!(feature = "strict_annotation_checks") {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
object_enumerator as *mut core::ffi::c_void,
std::mem::size_of::<TEE_ObjectEnumHandle>(),
);
if res != 0 {
eprintln!("[out] objectEnumerator: error {:#010x}", res);
TEE_Panic(0);
}
}
let mut oe: u32 = 0;
let res = unsafe { _utee_storage_alloc_enum(&mut oe) } as TEE_Result;
if res != TEE_SUCCESS {
oe = TEE_HANDLE_NULL as u32;
}
unsafe {
*object_enumerator = oe as TEE_ObjectEnumHandle;
}
if res != TEE_SUCCESS && res != TEE_ERROR_ACCESS_CONFLICT {
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_FreePersistentObjectEnumerator(object_enumerator: TEE_ObjectEnumHandle) {
if object_enumerator.is_null() {
return;
}
let res = unsafe { _utee_storage_free_enum(object_enumerator as u64) } as TEE_Result;
if res != TEE_SUCCESS {
TEE_Panic(res as u32);
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_ResetPersistentObjectEnumerator(object_enumerator: TEE_ObjectEnumHandle) {
if object_enumerator.is_null() {
return;
}
let res = unsafe { _utee_storage_reset_enum(object_enumerator as u64) } as TEE_Result;
if res != TEE_SUCCESS {
TEE_Panic(res as u32);
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_StartPersistentObjectEnumerator(
object_enumerator: TEE_ObjectEnumHandle,
storage_id: u32,
) -> TEE_Result {
let res = unsafe {
_utee_storage_start_enum(object_enumerator as u64, storage_id as u64) as TEE_Result
};
if res != TEE_SUCCESS
&& res != TEE_ERROR_ITEM_NOT_FOUND
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetNextPersistentObject(
object_enumerator: TEE_ObjectEnumHandle,
object_info: *mut TEE_ObjectInfo,
object_id: *mut core::ffi::c_void,
object_id_len: *mut usize,
) -> TEE_Result {
if cfg!(feature = "strict_annotation_checks") {
if !object_info.is_null() {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
object_info as *mut core::ffi::c_void,
std::mem::size_of::<TEE_ObjectInfo>(),
);
if res != 0 {
eprintln!("[out] objectInfo: error {:#010x}", res);
TEE_Panic(0);
}
}
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
object_id_len as *mut core::ffi::c_void,
std::mem::size_of::<usize>(),
);
if res != 0 {
eprintln!("[out] objectIDLen: error {:#010x}", res);
TEE_Panic(0);
}
}
if object_id.is_null() {
return TEE_ERROR_BAD_PARAMETERS;
}
let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
let mut len: u64 = 0;
unsafe {
if !object_id_len.is_null() {
len = *object_id_len as u64;
}
}
let res = unsafe {
_utee_storage_next_enum(object_enumerator as u64, &mut info, object_id, &mut len)
} as TEE_Result;
if !object_info.is_null() {
unsafe {
(*object_info).objectType = info.obj_type;
(*object_info).objectSize = info.obj_size;
(*object_info).maxObjectSize = info.max_obj_size;
(*object_info).objectUsage = info.obj_usage;
(*object_info).dataSize = info.data_size as usize;
(*object_info).dataPosition = info.data_pos as usize;
(*object_info).handleFlags = info.handle_flags;
}
}
unsafe {
if !object_id_len.is_null() {
*object_id_len = len as usize;
}
}
if res != TEE_SUCCESS
&& res != TEE_ERROR_ITEM_NOT_FOUND
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_ReadObjectData(
object: TEE_ObjectHandle,
buffer: *mut core::ffi::c_void,
size: usize,
count: *mut usize,
) -> TEE_Result {
if object.is_null() {
return TEE_ERROR_BAD_PARAMETERS;
}
if cfg!(feature = "strict_annotation_checks") {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
count as *mut core::ffi::c_void,
std::mem::size_of::<usize>(),
);
if res != 0 {
eprintln!("[out] count: error {:#010x}", res);
TEE_Panic(0);
}
}
let mut cnt64: u64 = 0;
unsafe {
if !count.is_null() {
cnt64 = *count as u64;
}
}
let res =
unsafe { _utee_storage_obj_read(object as u64, buffer, size, &mut cnt64) } as TEE_Result;
unsafe {
if !count.is_null() {
*count = cnt64 as usize;
}
}
if res != TEE_SUCCESS
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_WriteObjectData(
object: TEE_ObjectHandle,
buffer: *const core::ffi::c_void,
size: usize,
) -> TEE_Result {
if object.is_null() {
return TEE_ERROR_BAD_PARAMETERS;
}
if size > TEE_DATA_MAX_POSITION as usize {
return TEE_ERROR_OVERFLOW;
}
if cfg!(feature = "strict_annotation_checks") && size > 0 && !buffer.is_null() {
let res = TEE_CheckMemoryAccessRights(
TEE_MEMORY_ACCESS_READ,
buffer as *mut core::ffi::c_void,
size,
);
if res != 0 {
eprintln!("[in] buffer: error {:#010x}", res);
TEE_Panic(0);
}
}
let res = unsafe { _utee_storage_obj_write(object as u64, buffer, size) } as TEE_Result;
if res != TEE_SUCCESS
&& res != TEE_ERROR_STORAGE_NO_SPACE
&& res != TEE_ERROR_OVERFLOW
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_TruncateObjectData(object: TEE_ObjectHandle, size: usize) -> TEE_Result {
if object.is_null() {
return TEE_ERROR_BAD_PARAMETERS;
}
let res = unsafe {
_utee_storage_obj_trunc(
object as u64,
size, )
} as TEE_Result;
if res != TEE_SUCCESS
&& res != TEE_ERROR_STORAGE_NO_SPACE
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res as u32);
}
res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_SeekObjectData(
object: TEE_ObjectHandle,
offset: i64, whence: TEE_Whence,
) -> TEE_Result {
if object.is_null() {
return TEE_ERROR_BAD_PARAMETERS;
}
let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
let mut res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) as TEE_Result };
if res != TEE_SUCCESS {
return res;
}
let whence_u32 = whence as u32;
let whence_u64 = whence as u64;
match whence_u32 {
TEE_DATA_SEEK_SET => {
if offset > 0 && offset as u32 > TEE_DATA_MAX_POSITION {
return TEE_ERROR_OVERFLOW;
}
}
TEE_DATA_SEEK_CUR => {
if offset > 0
&& (offset as u32 + info.data_pos > TEE_DATA_MAX_POSITION as u32
|| offset as u32 + info.data_pos < info.data_pos)
{
return TEE_ERROR_OVERFLOW;
}
}
TEE_DATA_SEEK_END => {
if offset > 0
&& (offset as u32 + info.data_size > TEE_DATA_MAX_POSITION as u32
|| offset as u32 + info.data_size < info.data_size)
{
return TEE_ERROR_OVERFLOW;
}
}
_ => {
return TEE_ERROR_ITEM_NOT_FOUND;
}
}
res = unsafe { _utee_storage_obj_seek(object as u64, offset as i32, whence_u64) as TEE_Result };
if res != TEE_SUCCESS
&& res != TEE_ERROR_OVERFLOW
&& res != TEE_ERROR_CORRUPT_OBJECT
&& res != TEE_ERROR_STORAGE_NOT_AVAILABLE
{
TEE_Panic(res as u32);
}
res
}