1use crate::api::tee_api_mm::TEE_CheckMemoryAccessRights;
8use crate::api::tee_api_panic::TEE_Panic;
9use crate::syscalls::syscall_table::{
10 _utee_cryp_obj_alloc, _utee_cryp_obj_close, _utee_cryp_obj_copy, _utee_cryp_obj_generate_key,
11 _utee_cryp_obj_get_attr, _utee_cryp_obj_get_info, _utee_cryp_obj_populate,
12 _utee_cryp_obj_reset, _utee_cryp_obj_restrict_usage, _utee_storage_alloc_enum,
13 _utee_storage_free_enum, _utee_storage_next_enum, _utee_storage_obj_create,
14 _utee_storage_obj_del, _utee_storage_obj_open, _utee_storage_obj_read, _utee_storage_obj_rename,
15 _utee_storage_obj_seek, _utee_storage_obj_trunc, _utee_storage_obj_write,
16 _utee_storage_reset_enum,
17 _utee_storage_start_enum,
18};
19use crate::tee_api_defines::*;
20use crate::tee_api_types::{
21 TEE_Attribute, TEE_ObjectEnumHandle, TEE_ObjectHandle, TEE_ObjectInfo, TEE_Result, TEE_Whence,
22};
23use crate::utee_types::utee_attribute;
24
25pub const TEE_USAGE_DEFAULT: u32 = 0xffffffff;
27
28pub unsafe fn __utee_from_attr(
30 ua: *mut utee_attribute,
31 attrs: *const TEE_Attribute,
32 attr_count: u32,
33) {
34 unsafe {
35 for n in 0..attr_count as usize {
36 let ua_ptr = ua.add(n);
37 let attr_ptr = attrs.add(n);
38
39 (*ua_ptr).attribute_id = (*attr_ptr).attributeID;
40
41 if (*attr_ptr).attributeID & TEE_ATTR_FLAG_VALUE != 0 {
42 (*ua_ptr).a = (*attr_ptr).content.value.a as u64;
44 (*ua_ptr).b = (*attr_ptr).content.value.b as u64;
45 } else {
46 (*ua_ptr).a = (*attr_ptr).content.memref.buffer as u64;
48 (*ua_ptr).b = (*attr_ptr).content.memref.size as u64;
49 }
50 }
51 }
52}
53
54#[unsafe(no_mangle)]
58pub extern "C" fn TEE_GetObjectInfo(object: TEE_ObjectHandle, object_info: &mut TEE_ObjectInfo) {
59 let mut info = unsafe { std::mem::zeroed() }; let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) };
61
62 if res != TEE_SUCCESS as usize {
63 TEE_Panic(res as u32);
64 }
65
66 if info.obj_type == TEE_TYPE_CORRUPTED_OBJECT {
67 object_info.objectSize = 0;
69 object_info.maxObjectSize = 0;
70 object_info.objectUsage = 0;
71 object_info.dataSize = 0;
72 object_info.dataPosition = 0;
73 object_info.handleFlags = 0;
74 } else {
75 object_info.objectType = info.obj_type;
77 object_info.objectSize = info.obj_size;
78 object_info.maxObjectSize = info.max_obj_size;
79 object_info.objectUsage = info.obj_usage;
80 object_info.dataSize = info.data_size as usize;
81 object_info.dataPosition = info.data_pos as usize;
82 object_info.handleFlags = info.handle_flags;
83 }
84}
85
86#[unsafe(no_mangle)]
90pub extern "C" fn TEE_GetObjectInfo1(
91 object: TEE_ObjectHandle,
92 object_info: &mut TEE_ObjectInfo,
93) -> TEE_Result {
94 let mut info = unsafe { std::mem::zeroed() };
95 let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
96
97 if res != TEE_SUCCESS
99 && res != TEE_ERROR_CORRUPT_OBJECT
100 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
101 {
102 TEE_Panic(res as u32);
103 }
104
105 object_info.objectType = info.obj_type;
107 object_info.objectSize = info.obj_size;
108 object_info.maxObjectSize = info.max_obj_size;
109 object_info.objectUsage = info.obj_usage;
110 object_info.dataSize = info.data_size as usize;
111 object_info.dataPosition = info.data_pos as usize;
112 object_info.handleFlags = info.handle_flags;
113
114 res
115}
116
117#[unsafe(no_mangle)]
121pub extern "C" fn TEE_RestrictObjectUsage(object: TEE_ObjectHandle, object_usage: u32) {
122 let mut info = unsafe { std::mem::zeroed() };
123 unsafe {
124 _utee_cryp_obj_get_info(object as u64, &mut info);
125 }
126
127 if info.obj_type == TEE_TYPE_CORRUPTED_OBJECT {
129 return;
130 }
131
132 let res = TEE_RestrictObjectUsage1(object, object_usage);
133
134 if res != TEE_SUCCESS {
135 TEE_Panic(res as u32);
136 }
137}
138
139#[unsafe(no_mangle)]
143pub extern "C" fn TEE_RestrictObjectUsage1(
144 object: TEE_ObjectHandle,
145 object_usage: u32,
146) -> TEE_Result {
147 let res =
148 unsafe { _utee_cryp_obj_restrict_usage(object as u64, object_usage as u64) } as TEE_Result;
149
150 if res != TEE_SUCCESS
152 && res != TEE_ERROR_CORRUPT_OBJECT
153 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
154 {
155 TEE_Panic(res as u32);
156 }
157
158 res
159}
160
161#[unsafe(no_mangle)]
165pub extern "C" fn TEE_GetObjectBufferAttribute(
166 object: TEE_ObjectHandle,
167 attribute_id: u32,
168 buffer: *mut core::ffi::c_void,
169 size: *mut usize,
170) -> TEE_Result {
171 if cfg!(feature = "strict_annotation_checks") {
173 let res = TEE_CheckMemoryAccessRights(
174 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
175 size as *mut core::ffi::c_void,
176 std::mem::size_of::<usize>(),
177 );
178 if res != 0 {
179 eprintln!("[inout] size: error {:#010x}", res);
180 TEE_Panic(0);
181 }
182 }
183
184 let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
185 let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
186
187 if res != TEE_SUCCESS {
188 return check_result_and_panic(res);
189 }
190
191 if attribute_id & TEE_ATTR_FLAG_VALUE != 0 {
193 return check_result_and_panic(TEE_ERROR_BAD_PARAMETERS);
194 }
195
196 let mut buffer_size: u64 = 0;
197 unsafe {
198 if !size.is_null() {
199 buffer_size = *size as u64;
200 }
201 }
202
203 let res = unsafe {
204 _utee_cryp_obj_get_attr(object as u64, attribute_id as u64, buffer, &mut buffer_size)
205 } as TEE_Result;
206
207 unsafe {
208 if !size.is_null() {
209 *size = buffer_size as usize;
210 }
211 }
212
213 check_result_and_panic(res)
214}
215
216#[unsafe(no_mangle)]
220pub extern "C" fn TEE_GetObjectBufferAttribute1(
221 object: TEE_ObjectHandle,
222 attribute_id: u32,
223 buffer: *mut core::ffi::c_void,
224 size: *mut usize,
225) -> TEE_Result {
226 if size.is_null() {
228 return TEE_ERROR_BAD_PARAMETERS;
229 }
230
231 let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
232 let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
233
234 if res != TEE_SUCCESS {
235 return check_result_and_panic(res);
236 }
237
238 if attribute_id & TEE_ATTR_FLAG_VALUE != 0 {
240 return TEE_ERROR_BAD_PARAMETERS;
241 }
242
243 let mut required_size: u64 = 0;
245 let res = unsafe {
246 _utee_cryp_obj_get_attr(
247 object as u64,
248 attribute_id as u64,
249 core::ptr::null_mut(),
250 &mut required_size,
251 ) as TEE_Result
252 };
253
254 if res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER {
255 unsafe {
256 *size = required_size as usize;
257 }
258 } else {
259 return check_result_and_panic(res);
260 }
261
262 if !buffer.is_null() && unsafe { *size } >= required_size as usize {
264 let res = unsafe {
265 _utee_cryp_obj_get_attr(
266 object as u64,
267 attribute_id as u64,
268 buffer,
269 &mut required_size,
270 ) as TEE_Result
271 };
272
273 if res != TEE_SUCCESS {
274 return check_result_and_panic(res);
275 }
276
277 unsafe {
278 *size = required_size as usize;
279 }
280 }
281
282 TEE_SUCCESS
283}
284
285fn check_result_and_panic(res: TEE_Result) -> TEE_Result {
287 if res != TEE_SUCCESS
288 && res != TEE_ERROR_ITEM_NOT_FOUND
289 && res != TEE_ERROR_SHORT_BUFFER
290 && res != TEE_ERROR_CORRUPT_OBJECT
291 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
292 {
293 TEE_Panic(res as u32);
294 }
295 res
296}
297
298fn handle_result_and_return(
300 res: TEE_Result,
301 a: *mut u32,
302 b: *mut u32,
303 value_a: u32,
304 value_b: u32,
305) -> TEE_Result {
306 if res != TEE_SUCCESS
307 && res != TEE_ERROR_ITEM_NOT_FOUND
308 && res != TEE_ERROR_CORRUPT_OBJECT
309 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
310 {
311 TEE_Panic(res);
312 }
313
314 if res == TEE_SUCCESS {
315 if !a.is_null() {
316 unsafe { *a = value_a };
317 }
318 if !b.is_null() {
319 unsafe { *b = value_b };
320 }
321 }
322
323 res
324}
325
326#[unsafe(no_mangle)]
330pub extern "C" fn TEE_GetObjectValueAttribute(
331 object: TEE_ObjectHandle,
332 attribute_id: u32,
333 a: *mut u32,
334 b: *mut u32,
335) -> TEE_Result {
336 if cfg!(feature = "strict_annotation_checks") {
338 if !a.is_null() {
339 let res = TEE_CheckMemoryAccessRights(
340 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
341 a as *mut core::ffi::c_void,
342 std::mem::size_of::<u32>(),
343 );
344 if res != 0 {
345 eprintln!("[inout] a: error {:#010x}", res);
346 TEE_Panic(0);
347 }
348 }
349 if !b.is_null() {
350 let res = TEE_CheckMemoryAccessRights(
351 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
352 b as *mut core::ffi::c_void,
353 std::mem::size_of::<u32>(),
354 );
355 if res != 0 {
356 eprintln!("[inout] b: error {:#010x}", res);
357 TEE_Panic(0);
358 }
359 }
360 }
361
362 let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
363 let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
364
365 if res != TEE_SUCCESS {
366 return handle_result_and_return(res, a, b, 0, 0);
367 }
368
369 if attribute_id & TEE_ATTR_FLAG_VALUE == 0 {
371 let res = TEE_ERROR_BAD_PARAMETERS;
372 return handle_result_and_return(res, a, b, 0, 0);
373 }
374
375 let mut buf = [0u32; 2];
377 let mut size = std::mem::size_of_val(&buf) as u64;
378
379 let res = unsafe {
380 _utee_cryp_obj_get_attr(
381 object as u64,
382 attribute_id as u64,
383 buf.as_mut_ptr() as *mut core::ffi::c_void,
384 &mut size,
385 ) as TEE_Result
386 };
387
388 if res != TEE_SUCCESS
390 && res != TEE_ERROR_ITEM_NOT_FOUND
391 && res != TEE_ERROR_CORRUPT_OBJECT
392 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
393 {
394 TEE_Panic(res);
395 }
396
397 if size != std::mem::size_of_val(&buf) as u64 {
399 TEE_Panic(0);
400 }
401
402 if res == TEE_SUCCESS {
404 if !a.is_null() {
405 unsafe { *a = buf[0] };
406 }
407 if !b.is_null() {
408 unsafe { *b = buf[1] };
409 }
410 }
411
412 res
413}
414
415#[unsafe(no_mangle)]
419pub extern "C" fn TEE_CloseObject(object: TEE_ObjectHandle) {
420 if object.is_null() {
422 return;
423 }
424
425 let res = unsafe { _utee_cryp_obj_close(object as u64) } as TEE_Result;
426
427 if res != TEE_SUCCESS {
429 TEE_Panic(res as u32);
430 }
431}
432
433#[unsafe(no_mangle)]
437pub extern "C" fn TEE_AllocateTransientObject(
438 object_type: u32, max_object_size: u32,
440 object: *mut TEE_ObjectHandle,
441) -> TEE_Result {
442 if object_type == TEE_TYPE_DATA {
444 return TEE_ERROR_NOT_SUPPORTED;
445 }
446
447 if cfg!(feature = "strict_annotation_checks") {
449 let res = TEE_CheckMemoryAccessRights(
450 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
451 object as *mut core::ffi::c_void,
452 std::mem::size_of::<TEE_ObjectHandle>(),
453 );
454 if res != 0 {
455 eprintln!("[inout] object: error {:#010x}", res);
456 TEE_Panic(0);
457 }
458 }
459
460 let mut obj: u32 = 0;
461
462 let res = unsafe { _utee_cryp_obj_alloc(object_type as u64, max_object_size as u64, &mut obj) }
463 as TEE_Result;
464
465 if res != TEE_SUCCESS && res != TEE_ERROR_OUT_OF_MEMORY && res != TEE_ERROR_NOT_SUPPORTED {
467 TEE_Panic(res as u32);
468 }
469
470 if res == TEE_SUCCESS {
472 unsafe {
473 *object = obj as TEE_ObjectHandle;
474 }
475 }
476
477 res
478}
479
480#[unsafe(no_mangle)]
484pub extern "C" fn TEE_FreeTransientObject(object: TEE_ObjectHandle) {
485 if object.is_null() {
487 return;
488 }
489
490 let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
491 let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
492
493 if res != TEE_SUCCESS {
494 TEE_Panic(res as u32);
495 }
496
497 if (info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0 {
499 TEE_Panic(0);
500 }
501
502 let res = unsafe { _utee_cryp_obj_close(object as u64) } as TEE_Result;
503
504 if res != TEE_SUCCESS {
505 TEE_Panic(res as u32);
506 }
507}
508
509#[unsafe(no_mangle)]
513pub extern "C" fn TEE_ResetTransientObject(object: TEE_ObjectHandle) {
514 if object.is_null() {
516 return;
517 }
518
519 let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
520 let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
521
522 if res != TEE_SUCCESS {
523 TEE_Panic(res as u32);
524 }
525
526 if (info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0 {
528 TEE_Panic(0);
529 }
530
531 let res = unsafe { _utee_cryp_obj_reset(object as u64) } as TEE_Result;
532
533 if res != TEE_SUCCESS {
534 TEE_Panic(res as u32);
535 }
536}
537
538#[unsafe(no_mangle)]
542pub extern "C" fn TEE_PopulateTransientObject(
543 object: TEE_ObjectHandle,
544 attrs: *const TEE_Attribute,
545 attr_count: u32,
546) -> TEE_Result {
547 let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
551 let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
552
553 if res != TEE_SUCCESS {
554 TEE_Panic(res as u32);
555 }
556
557 if (info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0 {
559 TEE_Panic(0);
560 }
561
562 if (info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0 {
564 TEE_Panic(0);
565 }
566
567 let mut ua = vec![utee_attribute::default(); attr_count as usize];
569
570 unsafe {
571 __utee_from_attr(ua.as_mut_ptr(), attrs, attr_count);
572 }
573
574 let res = unsafe {
575 _utee_cryp_obj_populate(object as u64, ua.as_mut_ptr(), attr_count as u64) as TEE_Result
576 };
577
578 if res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS {
579 TEE_Panic(res as u32);
580 }
581
582 res
583}
584
585#[unsafe(no_mangle)]
589pub extern "C" fn TEE_InitRefAttribute(
590 attr: *mut TEE_Attribute,
591 attribute_id: u32,
592 buffer: *const core::ffi::c_void,
593 length: usize,
594) {
595 if cfg!(feature = "strict_annotation_checks") {
597 let res = TEE_CheckMemoryAccessRights(
598 TEE_MEMORY_ACCESS_WRITE,
599 attr as *mut core::ffi::c_void,
600 std::mem::size_of::<TEE_Attribute>(),
601 );
602 if res != 0 {
603 eprintln!("[out] attr: error {:#010x}", res);
604 TEE_Panic(0);
605 }
606 }
607
608 if (attribute_id & TEE_ATTR_FLAG_VALUE) != 0 {
610 TEE_Panic(0);
611 }
612
613 unsafe {
615 if !attr.is_null() {
616 (*attr).attributeID = attribute_id;
617 (*attr).content.memref.buffer = buffer as *mut core::ffi::c_void;
618 (*attr).content.memref.size = length;
619 }
620 }
621}
622
623#[unsafe(no_mangle)]
627pub extern "C" fn TEE_InitValueAttribute(
628 attr: *mut TEE_Attribute,
629 attribute_id: u32,
630 a: u32,
631 b: u32,
632) {
633 if cfg!(feature = "strict_annotation_checks") {
635 let res = TEE_CheckMemoryAccessRights(
636 TEE_MEMORY_ACCESS_WRITE,
637 attr as *mut core::ffi::c_void,
638 std::mem::size_of::<TEE_Attribute>(),
639 );
640 if res != 0 {
641 eprintln!("[out] attr: error {:#010x}", res);
642 TEE_Panic(0);
643 }
644 }
645
646 if (attribute_id & TEE_ATTR_FLAG_VALUE) == 0 {
648 TEE_Panic(0);
649 }
650
651 unsafe {
653 if !attr.is_null() {
654 (*attr).attributeID = attribute_id;
655 (*attr).content.value.a = a;
656 (*attr).content.value.b = b;
657 }
658 }
659}
660
661#[unsafe(no_mangle)]
665pub extern "C" fn TEE_CopyObjectAttributes(
666 dest_object: TEE_ObjectHandle,
667 src_object: TEE_ObjectHandle,
668) {
669 let mut src_info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
670 let _res = unsafe { _utee_cryp_obj_get_info(src_object as u64, &mut src_info) } as TEE_Result;
671
672 if src_info.obj_type == TEE_TYPE_CORRUPTED_OBJECT {
674 return;
675 }
676
677 let res = TEE_CopyObjectAttributes1(dest_object, src_object);
678
679 if res != TEE_SUCCESS {
680 TEE_Panic(res as u32);
681 }
682}
683
684#[unsafe(no_mangle)]
688pub extern "C" fn TEE_CopyObjectAttributes1(
689 dest_object: TEE_ObjectHandle,
690 src_object: TEE_ObjectHandle,
691) -> TEE_Result {
692 let mut dst_info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
693 let mut src_info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
694
695 let mut res =
697 unsafe { _utee_cryp_obj_get_info(dest_object as u64, &mut dst_info) } as TEE_Result;
698
699 if res != TEE_SUCCESS {
700 return check_copy_object_attributes_result(res);
701 }
702
703 res = unsafe { _utee_cryp_obj_get_info(src_object as u64, &mut src_info) } as TEE_Result;
705
706 if res != TEE_SUCCESS {
707 return check_copy_object_attributes_result(res);
708 }
709
710 if (src_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
712 TEE_Panic(0);
713 }
714
715 if (dst_info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0 {
717 TEE_Panic(0);
718 }
719
720 if (dst_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0 {
722 TEE_Panic(0);
723 }
724
725 res = unsafe { _utee_cryp_obj_copy(dest_object as u64, src_object as u64) } as TEE_Result;
727
728 check_copy_object_attributes_result(res)
729}
730
731fn check_copy_object_attributes_result(res: TEE_Result) -> TEE_Result {
733 if res != TEE_SUCCESS
734 && res != TEE_ERROR_CORRUPT_OBJECT
735 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
736 {
737 TEE_Panic(res as u32);
738 }
739
740 res
741}
742
743#[unsafe(no_mangle)]
747pub extern "C" fn TEE_GenerateKey(
748 object: TEE_ObjectHandle,
749 key_size: u32,
750 params: *const TEE_Attribute,
751 param_count: u32,
752) -> TEE_Result {
753 if cfg!(feature = "strict_annotation_checks") && param_count > 0 {
755 let res = TEE_CheckMemoryAccessRights(
756 TEE_MEMORY_ACCESS_READ,
757 params as *mut core::ffi::c_void,
758 std::mem::size_of::<TEE_Attribute>() * param_count as usize,
759 );
760 if res != 0 {
761 eprintln!("[in] attrs: error {:#010x}", res);
762 TEE_Panic(0);
763 }
764 }
765
766 let mut ua = vec![utee_attribute::default(); param_count as usize];
768
769 unsafe {
770 __utee_from_attr(ua.as_mut_ptr(), params, param_count);
771 }
772
773 let res = unsafe {
774 _utee_cryp_obj_generate_key(
775 object as u64,
776 key_size as u64,
777 ua.as_ptr(),
778 param_count as u64,
779 )
780 } as TEE_Result;
781
782 if res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS {
783 TEE_Panic(res as u32);
784 }
785
786 res
787}
788
789#[unsafe(no_mangle)]
793pub extern "C" fn TEE_OpenPersistentObject(
794 storage_id: u32,
795 object_id: *const core::ffi::c_void,
796 object_id_len: usize,
797 flags: u32,
798 object: *mut TEE_ObjectHandle,
799) -> TEE_Result {
800 if cfg!(feature = "strict_annotation_checks") {
802 let res = TEE_CheckMemoryAccessRights(
803 TEE_MEMORY_ACCESS_WRITE,
804 object as *mut core::ffi::c_void,
805 std::mem::size_of::<TEE_ObjectHandle>(),
806 );
807 if res != 0 {
808 eprintln!("[out] object: error {:#010x}", res);
809 TEE_Panic(0);
810 }
811 }
812
813 let mut obj: u32 = 0;
814
815 let res = unsafe {
816 _utee_storage_obj_open(
817 storage_id as u64,
818 object_id,
819 object_id_len,
820 flags as u64,
821 &mut obj,
822 )
823 } as TEE_Result;
824
825 if res == TEE_SUCCESS {
827 unsafe {
828 *object = obj as TEE_ObjectHandle;
829 }
830 } else {
831 unsafe {
833 *object = core::ptr::null_mut();
834 }
835 }
836
837 if res != TEE_SUCCESS
839 && res != TEE_ERROR_ITEM_NOT_FOUND
840 && res != TEE_ERROR_ACCESS_CONFLICT
841 && res != TEE_ERROR_OUT_OF_MEMORY
842 && res != TEE_ERROR_CORRUPT_OBJECT
843 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
844 {
845 TEE_Panic(res as u32);
846 }
847
848 res
849}
850
851#[unsafe(no_mangle)]
855pub extern "C" fn TEE_CreatePersistentObject(
856 storage_id: u32,
857 object_id: *const core::ffi::c_void,
858 object_id_len: usize,
859 flags: u32,
860 attributes: TEE_ObjectHandle,
861 initial_data: *const core::ffi::c_void,
862 initial_data_len: usize,
863 object: *mut TEE_ObjectHandle,
864) -> TEE_Result {
865 let mut obj: u32 = 0;
866 let obj_ptr: *mut u32;
867
868 if !object.is_null() {
870 if cfg!(feature = "strict_annotation_checks") {
871 let res = TEE_CheckMemoryAccessRights(
872 TEE_MEMORY_ACCESS_WRITE,
873 object as *mut core::ffi::c_void,
874 std::mem::size_of::<TEE_ObjectHandle>(),
875 );
876 if res != 0 {
877 eprintln!("[out] object: error {:#010x}", res);
878 TEE_Panic(0);
879 }
880 }
881 obj_ptr = &mut obj;
882 } else {
883 obj_ptr = core::ptr::null_mut();
884 }
885
886 let res = unsafe {
887 _utee_storage_obj_create(
888 storage_id as u64,
889 object_id,
890 object_id_len,
891 flags as u64,
892 attributes as u64,
893 initial_data,
894 initial_data_len,
895 obj_ptr,
896 )
897 } as TEE_Result;
898
899 if res == TEE_SUCCESS && !object.is_null() {
901 unsafe {
902 *object = obj as TEE_ObjectHandle;
903 }
904 } else if res != TEE_SUCCESS && !object.is_null() {
905 unsafe {
907 *object = core::ptr::null_mut();
908 }
909 }
910
911 if res != TEE_SUCCESS
913 && res != TEE_ERROR_ITEM_NOT_FOUND
914 && res != TEE_ERROR_ACCESS_CONFLICT
915 && res != TEE_ERROR_OUT_OF_MEMORY
916 && res != TEE_ERROR_STORAGE_NO_SPACE
917 && res != TEE_ERROR_CORRUPT_OBJECT
918 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
919 {
920 TEE_Panic(res as u32);
921 }
922
923 res
924}
925
926#[unsafe(no_mangle)]
930pub extern "C" fn TEE_CloseAndDeletePersistentObject(object: TEE_ObjectHandle) {
931 if object.is_null() {
933 return;
934 }
935
936 let res = TEE_CloseAndDeletePersistentObject1(object);
937
938 if res != TEE_SUCCESS {
939 TEE_Panic(0);
940 }
941}
942
943#[unsafe(no_mangle)]
947pub extern "C" fn TEE_CloseAndDeletePersistentObject1(object: TEE_ObjectHandle) -> TEE_Result {
948 if object.is_null() {
950 return TEE_SUCCESS;
951 }
952
953 let res = unsafe { _utee_storage_obj_del(object as u64) } as TEE_Result;
954
955 if res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE {
957 TEE_Panic(res as u32);
958 }
959
960 res
961}
962
963#[unsafe(no_mangle)]
967pub extern "C" fn TEE_RenamePersistentObject(
968 object: TEE_ObjectHandle,
969 new_object_id: *const core::ffi::c_void,
970 new_object_id_len: usize,
971) -> TEE_Result {
972 let res = if object.is_null() {
973 TEE_ERROR_BAD_PARAMETERS
974 } else {
975 unsafe {
976 _utee_storage_obj_rename(object as u64, new_object_id, new_object_id_len) as TEE_Result
977 }
978 };
979
980 if res != TEE_SUCCESS
981 && res != TEE_ERROR_ACCESS_CONFLICT
982 && res != TEE_ERROR_CORRUPT_OBJECT
983 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
984 && res != TEE_ERROR_BAD_PARAMETERS
985 {
986 TEE_Panic(res as u32);
987 }
988
989 res
990}
991
992#[unsafe(no_mangle)]
996pub extern "C" fn TEE_AllocatePersistentObjectEnumerator(
997 object_enumerator: *mut TEE_ObjectEnumHandle,
998) -> TEE_Result {
999 if cfg!(feature = "strict_annotation_checks") {
1001 let res = TEE_CheckMemoryAccessRights(
1002 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
1003 object_enumerator as *mut core::ffi::c_void,
1004 std::mem::size_of::<TEE_ObjectEnumHandle>(),
1005 );
1006 if res != 0 {
1007 eprintln!("[out] objectEnumerator: error {:#010x}", res);
1008 TEE_Panic(0);
1009 }
1010 }
1011
1012 let mut oe: u32 = 0;
1013
1014 let res = unsafe { _utee_storage_alloc_enum(&mut oe) } as TEE_Result;
1015
1016 if res != TEE_SUCCESS {
1018 oe = TEE_HANDLE_NULL as u32;
1019 }
1020
1021 unsafe {
1023 *object_enumerator = oe as TEE_ObjectEnumHandle;
1024 }
1025
1026 if res != TEE_SUCCESS && res != TEE_ERROR_ACCESS_CONFLICT {
1028 TEE_Panic(res as u32);
1029 }
1030
1031 res
1032}
1033
1034#[unsafe(no_mangle)]
1038pub extern "C" fn TEE_FreePersistentObjectEnumerator(object_enumerator: TEE_ObjectEnumHandle) {
1039 if object_enumerator.is_null() {
1041 return;
1042 }
1043
1044 let res = unsafe { _utee_storage_free_enum(object_enumerator as u64) } as TEE_Result;
1045
1046 if res != TEE_SUCCESS {
1047 TEE_Panic(res as u32);
1048 }
1049}
1050
1051#[unsafe(no_mangle)]
1055pub extern "C" fn TEE_ResetPersistentObjectEnumerator(object_enumerator: TEE_ObjectEnumHandle) {
1056 if object_enumerator.is_null() {
1058 return;
1059 }
1060
1061 let res = unsafe { _utee_storage_reset_enum(object_enumerator as u64) } as TEE_Result;
1062
1063 if res != TEE_SUCCESS {
1064 TEE_Panic(res as u32);
1065 }
1066}
1067
1068#[unsafe(no_mangle)]
1072pub extern "C" fn TEE_StartPersistentObjectEnumerator(
1073 object_enumerator: TEE_ObjectEnumHandle,
1074 storage_id: u32,
1075) -> TEE_Result {
1076 let res = unsafe {
1077 _utee_storage_start_enum(object_enumerator as u64, storage_id as u64) as TEE_Result
1078 };
1079
1080 if res != TEE_SUCCESS
1081 && res != TEE_ERROR_ITEM_NOT_FOUND
1082 && res != TEE_ERROR_CORRUPT_OBJECT
1083 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
1084 {
1085 TEE_Panic(res);
1086 }
1087
1088 res
1089}
1090
1091#[unsafe(no_mangle)]
1095pub extern "C" fn TEE_GetNextPersistentObject(
1096 object_enumerator: TEE_ObjectEnumHandle,
1097 object_info: *mut TEE_ObjectInfo,
1098 object_id: *mut core::ffi::c_void,
1099 object_id_len: *mut usize,
1100) -> TEE_Result {
1101 if cfg!(feature = "strict_annotation_checks") {
1103 if !object_info.is_null() {
1105 let res = TEE_CheckMemoryAccessRights(
1106 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
1107 object_info as *mut core::ffi::c_void,
1108 std::mem::size_of::<TEE_ObjectInfo>(),
1109 );
1110 if res != 0 {
1111 eprintln!("[out] objectInfo: error {:#010x}", res);
1112 TEE_Panic(0);
1113 }
1114 }
1115
1116 let res = TEE_CheckMemoryAccessRights(
1118 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
1119 object_id_len as *mut core::ffi::c_void,
1120 std::mem::size_of::<usize>(),
1121 );
1122 if res != 0 {
1123 eprintln!("[out] objectIDLen: error {:#010x}", res);
1124 TEE_Panic(0);
1125 }
1126 }
1127
1128 if object_id.is_null() {
1130 return TEE_ERROR_BAD_PARAMETERS;
1131 }
1132
1133 let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
1134 let mut len: u64 = 0;
1135
1136 unsafe {
1137 if !object_id_len.is_null() {
1138 len = *object_id_len as u64;
1139 }
1140 }
1141
1142 let res = unsafe {
1143 _utee_storage_next_enum(object_enumerator as u64, &mut info, object_id, &mut len)
1144 } as TEE_Result;
1145
1146 if !object_info.is_null() {
1148 unsafe {
1149 (*object_info).objectType = info.obj_type;
1150 (*object_info).objectSize = info.obj_size;
1151 (*object_info).maxObjectSize = info.max_obj_size;
1152 (*object_info).objectUsage = info.obj_usage;
1153 (*object_info).dataSize = info.data_size as usize;
1154 (*object_info).dataPosition = info.data_pos as usize;
1155 (*object_info).handleFlags = info.handle_flags;
1156 }
1157 }
1158
1159 unsafe {
1161 if !object_id_len.is_null() {
1162 *object_id_len = len as usize;
1163 }
1164 }
1165
1166 if res != TEE_SUCCESS
1168 && res != TEE_ERROR_ITEM_NOT_FOUND
1169 && res != TEE_ERROR_CORRUPT_OBJECT
1170 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
1171 {
1172 TEE_Panic(res as u32);
1173 }
1174
1175 res
1176}
1177
1178#[unsafe(no_mangle)]
1182pub extern "C" fn TEE_ReadObjectData(
1183 object: TEE_ObjectHandle,
1184 buffer: *mut core::ffi::c_void,
1185 size: usize,
1186 count: *mut usize,
1187) -> TEE_Result {
1188 if object.is_null() {
1190 return TEE_ERROR_BAD_PARAMETERS;
1191 }
1192
1193 if cfg!(feature = "strict_annotation_checks") {
1195 let res = TEE_CheckMemoryAccessRights(
1196 TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
1197 count as *mut core::ffi::c_void,
1198 std::mem::size_of::<usize>(),
1199 );
1200 if res != 0 {
1201 eprintln!("[out] count: error {:#010x}", res);
1202 TEE_Panic(0);
1203 }
1204 }
1205
1206 let mut cnt64: u64 = 0;
1207 unsafe {
1208 if !count.is_null() {
1209 cnt64 = *count as u64;
1210 }
1211 }
1212
1213 let res =
1214 unsafe { _utee_storage_obj_read(object as u64, buffer, size, &mut cnt64) } as TEE_Result;
1215
1216 unsafe {
1218 if !count.is_null() {
1219 *count = cnt64 as usize;
1220 }
1221 }
1222
1223 if res != TEE_SUCCESS
1225 && res != TEE_ERROR_CORRUPT_OBJECT
1226 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
1227 {
1228 TEE_Panic(res as u32);
1229 }
1230
1231 res
1232}
1233
1234#[unsafe(no_mangle)]
1238pub extern "C" fn TEE_WriteObjectData(
1239 object: TEE_ObjectHandle,
1240 buffer: *const core::ffi::c_void,
1241 size: usize,
1242) -> TEE_Result {
1243 if object.is_null() {
1245 return TEE_ERROR_BAD_PARAMETERS;
1246 }
1247
1248 if size > TEE_DATA_MAX_POSITION as usize {
1250 return TEE_ERROR_OVERFLOW;
1251 }
1252
1253 if cfg!(feature = "strict_annotation_checks") && size > 0 && !buffer.is_null() {
1255 let res = TEE_CheckMemoryAccessRights(
1256 TEE_MEMORY_ACCESS_READ,
1257 buffer as *mut core::ffi::c_void,
1258 size,
1259 );
1260 if res != 0 {
1261 eprintln!("[in] buffer: error {:#010x}", res);
1262 TEE_Panic(0);
1263 }
1264 }
1265
1266 let res = unsafe { _utee_storage_obj_write(object as u64, buffer, size) } as TEE_Result;
1267
1268 if res != TEE_SUCCESS
1270 && res != TEE_ERROR_STORAGE_NO_SPACE
1271 && res != TEE_ERROR_OVERFLOW
1272 && res != TEE_ERROR_CORRUPT_OBJECT
1273 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
1274 {
1275 TEE_Panic(res as u32);
1276 }
1277
1278 res
1279}
1280
1281#[unsafe(no_mangle)]
1285pub extern "C" fn TEE_TruncateObjectData(object: TEE_ObjectHandle, size: usize) -> TEE_Result {
1286 if object.is_null() {
1288 return TEE_ERROR_BAD_PARAMETERS;
1289 }
1290
1291 let res = unsafe {
1292 _utee_storage_obj_trunc(
1293 object as u64,
1294 size, )
1296 } as TEE_Result;
1297
1298 if res != TEE_SUCCESS
1300 && res != TEE_ERROR_STORAGE_NO_SPACE
1301 && res != TEE_ERROR_CORRUPT_OBJECT
1302 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
1303 {
1304 TEE_Panic(res as u32);
1305 }
1306
1307 res
1308}
1309
1310#[unsafe(no_mangle)]
1317pub extern "C" fn TEE_SeekObjectData(
1318 object: TEE_ObjectHandle,
1319 offset: i64, whence: TEE_Whence,
1321) -> TEE_Result {
1322 if object.is_null() {
1324 return TEE_ERROR_BAD_PARAMETERS;
1325 }
1326
1327 let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
1329 let mut res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) as TEE_Result };
1330
1331 if res != TEE_SUCCESS {
1334 if res != TEE_ERROR_CORRUPT_OBJECT && res != TEE_ERROR_STORAGE_NOT_AVAILABLE {
1335 TEE_Panic(res as u32);
1336 }
1337 return res;
1338 }
1339
1340 let whence_u32 = whence as u32;
1342 let whence_u64 = whence as u64;
1343
1344 match whence_u32 {
1346 TEE_DATA_SEEK_SET => {
1347 if offset > 0 && offset as u32 > TEE_DATA_MAX_POSITION {
1348 return TEE_ERROR_OVERFLOW;
1349 }
1350 }
1351 TEE_DATA_SEEK_CUR => {
1352 if offset > 0
1353 && (offset as u32 + info.data_pos > TEE_DATA_MAX_POSITION as u32
1354 || offset as u32 + info.data_pos < info.data_pos)
1355 {
1356 return TEE_ERROR_OVERFLOW;
1357 }
1358 }
1359 TEE_DATA_SEEK_END => {
1360 if offset > 0
1361 && (offset as u32 + info.data_size > TEE_DATA_MAX_POSITION as u32
1362 || offset as u32 + info.data_size < info.data_size)
1363 {
1364 return TEE_ERROR_OVERFLOW;
1365 }
1366 }
1367 _ => {
1368 return TEE_ERROR_ITEM_NOT_FOUND;
1369 }
1370 }
1371
1372 res = unsafe { _utee_storage_obj_seek(object as u64, offset as i32, whence_u64) as TEE_Result };
1374
1375 if res != TEE_SUCCESS
1377 && res != TEE_ERROR_OVERFLOW
1378 && res != TEE_ERROR_CORRUPT_OBJECT
1379 && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
1380 {
1381 TEE_Panic(res as u32);
1382 }
1383
1384 res
1385}