rust-libutee 0.1.0

Rust library for UTEE-related functionality.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
use crate::api::tee_api_mm::TEE_Free;
use crate::api::tee_api_mm::TEE_Malloc;
use crate::tee_api_defines::*;
use crate::tee_api_types::*;
use core::ffi::c_void;
use core::ffi::*;
use libc::strlen;
use mbedtls_sys_auto::base64_decode;
use mbedtls_sys_auto::base64_encode;
use std::ptr;
use std::ptr::null_mut;

use crate::api::tee_api_panic::TEE_Panic;
use crate::syscalls::syscall_table::{_utee_get_property, _utee_get_property_name_to_index};
use core::mem;

#[repr(C)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum UserTaPropType {
    USER_TA_PROP_TYPE_BOOL,         /* bool */
    USER_TA_PROP_TYPE_U32,          /* uint32_t */
    USER_TA_PROP_TYPE_UUID,         /* TEE_UUID */
    USER_TA_PROP_TYPE_IDENTITY,     /* TEE_Identity */
    USER_TA_PROP_TYPE_STRING,       /* zero terminated string of char */
    USER_TA_PROP_TYPE_BINARY_BLOCK, /* zero terminated base64 coded string */
    USER_TA_PROP_TYPE_U64,          /* uint64_t */
    USER_TA_PROP_TYPE_INVALID,      /* invalid value */
}

#[repr(C)]
pub struct UserTaProperty {
    pub name: *const c_char,
    pub prop_type: UserTaPropType,
    pub value: *mut c_void,
}

unsafe impl Sync for UserTaProperty {}

#[cfg(feature = "ta_props_empty_impl")]
#[unsafe(no_mangle)]
pub static ta_num_props: usize = 0;
#[cfg(feature = "ta_props_empty_impl")]
#[unsafe(no_mangle)]
pub static ta_props: UserTaProperty = UserTaProperty {
    name: core::ptr::null(),
    prop_type: UserTaPropType::USER_TA_PROP_TYPE_INVALID,
    value: core::ptr::null_mut(),
};

// Defined by the final TA crate (link-time symbols), not by rust-libutee.
#[cfg(not(feature = "ta_props_empty_impl"))]
unsafe extern "C" {
    static ta_num_props: usize;
    static ta_props: UserTaProperty;
}

#[repr(C)]
struct PropEnumerator {
    idx: u32,
    prop_set: TEE_PropSetHandle,
}

//TODO: this need to be a config option
pub const CFG_TA_BIGNUM_MAX_BITS: u32 = 2048;
pub const TEE_ISOCKET_VERSION: u32 = 0x01000000;
pub const PROP_ENUMERATOR_NOT_STARTED: u32 = 0xffffffff;
pub const TEE_USER_MEM_HINT_NO_FILL_ZERO: u32 = 0x80000000;

pub const TEE_CORE_API_MAJOR_VERSION: u32 = 1;
pub const TEE_CORE_API_MINOR_VERSION: u32 = 3;
pub const TEE_CORE_API_MAINTENANCE_VERSION: u32 = 1;
pub const MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL: i32 = -0x002A_i32;

pub const TEE_CORE_API_VERSION: u32 = (TEE_CORE_API_MAJOR_VERSION << 24)
    | (TEE_CORE_API_MINOR_VERSION << 16)
    | (TEE_CORE_API_MAINTENANCE_VERSION << 8);

#[unsafe(no_mangle)]
pub static tee_props: [UserTaProperty; 4usize] = [
    UserTaProperty {
        name: "gpd.tee.arith.maxBigIntSize\0".as_ptr() as _,
        prop_type: UserTaPropType::USER_TA_PROP_TYPE_U32,
        value: &CFG_TA_BIGNUM_MAX_BITS as *const u32 as *mut _,
    },
    UserTaProperty {
        name: "gpd.tee.sockets.version\0".as_ptr() as _,
        prop_type: UserTaPropType::USER_TA_PROP_TYPE_U32,
        value: &TEE_ISOCKET_VERSION as *const u32 as *mut _,
    },
    UserTaProperty {
        name: "gpd.tee.sockets.tcp.version\0".as_ptr() as _,
        prop_type: UserTaPropType::USER_TA_PROP_TYPE_U32,
        value: &TEE_ISOCKET_VERSION as *const u32 as *mut _,
    },
    UserTaProperty {
        name: "gpd.tee.internalCore.version\0".as_ptr() as _,
        prop_type: UserTaPropType::USER_TA_PROP_TYPE_U32,
        value: &TEE_CORE_API_VERSION as *const u32 as *mut _,
    },
];

fn is_propset_pseudo_handle(h: TEE_PropSetHandle) -> bool {
    h == TEE_PROPSET_CURRENT_TA
        || h == TEE_PROPSET_CURRENT_CLIENT
        || h == TEE_PROPSET_TEE_IMPLEMENTATION
}

fn propset_get(
    h: TEE_PropSetHandle,
    eps: *mut *const UserTaProperty,
    eps_len: *mut usize,
) -> TEE_Result {
    unsafe {
        match h {
            TEE_PROPSET_CURRENT_TA => {
                *eps = core::ptr::addr_of!(ta_props);
                *eps_len = ta_num_props;
            }
            TEE_PROPSET_CURRENT_CLIENT => {
                *eps = core::ptr::null();
                *eps_len = 0;
            }
            TEE_PROPSET_TEE_IMPLEMENTATION => {
                *eps = tee_props.as_ptr();
                *eps_len = tee_props.len();
            }
            _ => {
                return TEE_ERROR_ITEM_NOT_FOUND;
            }
        }
    }
    TEE_SUCCESS
}

#[inline]
pub unsafe fn base64_dec(
    src: *const libc::c_char,
    src_len: usize,
    dst: *mut u8,
    dst_len: &mut usize,
) -> bool {
    let mut out_len: usize = 0;

    let ret = unsafe { base64_decode(dst, *dst_len, &mut out_len, src as *const u8, src_len) };

    *dst_len = out_len;

    ret == 0 || ret == MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL
}

#[inline]
pub unsafe fn base64_enc(
    src: *const c_char,
    src_len: usize,
    dst: *mut u8,
    dst_len: &mut usize,
) -> bool {
    let mut out_len: usize = 0;

    let ret = unsafe { base64_encode(dst, *dst_len, &mut out_len, src as *const u8, src_len) };

    *dst_len = out_len;

    ret == 0 || ret == MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL
}

unsafe fn propget_get_ext_prop(
    prop: *const UserTaProperty,
    prop_type: *mut UserTaPropType,
    buf: *mut c_void,
    len: *mut c_uint,
) -> TEE_Result {
    let prop = unsafe { &*prop };
    unsafe { *prop_type = prop.prop_type };
    let l: usize = match prop.prop_type {
        UserTaPropType::USER_TA_PROP_TYPE_BOOL => mem::size_of::<bool>(),
        UserTaPropType::USER_TA_PROP_TYPE_U32 => mem::size_of::<u32>(),
        UserTaPropType::USER_TA_PROP_TYPE_UUID => mem::size_of::<TEE_UUID>(),
        UserTaPropType::USER_TA_PROP_TYPE_U64 => mem::size_of::<u64>(),
        UserTaPropType::USER_TA_PROP_TYPE_IDENTITY => mem::size_of::<TEE_Identity>(),
        UserTaPropType::USER_TA_PROP_TYPE_STRING => unsafe {
            strlen(prop.value as *const c_char) + 1
        },
        UserTaPropType::USER_TA_PROP_TYPE_BINARY_BLOCK => {
            let mut out_len = unsafe { *len as usize };
            let ok = unsafe {
                base64_dec(
                    prop.value as *const c_char,
                    strlen(prop.value as *const c_char),
                    buf as *mut u8,
                    &mut out_len,
                )
            };
            if !ok && out_len <= unsafe { *len as usize } {
                return TEE_ERROR_GENERIC;
            }
            if unsafe { *len as usize } < out_len {
                unsafe { *len = out_len as u32 };
                return TEE_ERROR_SHORT_BUFFER;
            }
            unsafe { *len = out_len as u32 };
            TEE_SUCCESS as usize
        }
        _ => TEE_ERROR_GENERIC as usize,
    };
    if unsafe { *len as usize } < l {
        unsafe { *len = l as u32 };
        return TEE_ERROR_SHORT_BUFFER;
    }

    unsafe { ptr::copy(prop.value as *const u8, buf as *mut u8, l) };

    TEE_SUCCESS
}

unsafe fn propget_get_property(
    h: TEE_PropSetHandle,
    name: *const c_char,
    prop_type_out: *mut UserTaPropType,
    buf: *mut c_void,
    len: *mut c_uint,
) -> TEE_Result {
    let mut eps: *const UserTaProperty = core::ptr::null();
    let mut eps_len: usize = 0;
    let mut prop_type: u32 = 0;
    let mut index: u32 = 0;

    if is_propset_pseudo_handle(h) {
        let res = propset_get(h, &mut eps, &mut eps_len);
        if res != TEE_SUCCESS {
            return res;
        }
        for n in 0..eps_len {
            let prop = unsafe { &*eps.add(n) };
            let s1 = unsafe { CStr::from_ptr(name).to_str().ok().unwrap() };
            let s2 = unsafe { CStr::from_ptr(prop.name).to_str().ok().unwrap() };
            if s1 == s2 {
                return unsafe { propget_get_ext_prop(prop, prop_type_out, buf, len) };
            }
        }

        let res = unsafe {
            _utee_get_property_name_to_index(
                h as u64,
                name as *const c_void,
                strlen(name) as u64 + 1,
                &mut index,
            )
        };
        if res != (TEE_SUCCESS as usize) {
            return res as u32;
        }

        let res = unsafe {
            _utee_get_property(
                h as u64,
                index as u64,
                null_mut(),
                null_mut(),
                buf,
                len,
                &mut prop_type,
            )
        };

        if prop_type_out.is_null() {
            return res as u32;
        }

        unsafe { *prop_type_out = core::mem::transmute(prop_type) };
        return res as u32;
    }

    let pe = unsafe { &*(h as *const PropEnumerator) };
    let mut idx = pe.idx;
    if idx == PROP_ENUMERATOR_NOT_STARTED {
        return TEE_ERROR_ITEM_NOT_FOUND;
    }

    let res = propset_get(pe.prop_set, &mut eps, &mut eps_len);
    if res != TEE_SUCCESS {
        return res;
    }

    if (idx as usize) < eps_len {
        return unsafe { propget_get_ext_prop(&*eps.add(idx as usize), prop_type_out, buf, len) };
    }
    idx -= eps_len as u32;

    let res = unsafe {
        _utee_get_property(
            pe.prop_set as u64,
            idx as u64,
            null_mut(),
            null_mut(),
            buf,
            len,
            &mut prop_type,
        )
    };

    let res = if res == (TEE_ERROR_ITEM_NOT_FOUND as usize) {
        TEE_ERROR_BAD_PARAMETERS
    } else {
        res as u32
    };

    if !prop_type_out.is_null() {
        unsafe { *prop_type_out = core::mem::transmute(prop_type) };
    }

    res
}

fn handle_result(res: TEE_Result, tmp_buf: *mut c_void) -> TEE_Result {
    if !tmp_buf.is_null() {
        TEE_Free(tmp_buf);
    }
    if res != TEE_SUCCESS && res != TEE_ERROR_SHORT_BUFFER && res != TEE_ERROR_ITEM_NOT_FOUND {
        TEE_Panic(0);
    }
    res
}
fn base64_enc_len(n: usize) -> usize {
    let enc = ((n + 2) / 3) * 4;
    enc + 1
}

fn copy_string(src: &str, dst: *mut u8) -> Result<usize, ()> {
    unsafe {
        core::ptr::copy_nonoverlapping(src.as_ptr(), dst, src.len());
    }

    Ok(src.len())
}

fn copy_constu8_string(src: *const u8, dst: *mut u8) -> Result<usize, ()> {
    let c_str = unsafe { core::ffi::CStr::from_ptr(src as _) };
    unsafe {
        core::ptr::copy_nonoverlapping(src, dst, c_str.to_bytes().len());
    }

    Ok(c_str.to_bytes().len())
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetPropertyAsString(
    propset_or_enumerator: TEE_PropSetHandle,
    name: *const c_char,
    value: *mut c_char,
    value_len: *mut usize,
) -> TEE_Result {
    let mut res;
    let tmp_len = if unsafe { *value_len } < mem::size_of::<TEE_Identity>() {
        mem::size_of::<TEE_Identity>()
    } else {
        unsafe { *value_len }
    };
    let tmp_buf = TEE_Malloc(tmp_len, TEE_USER_MEM_HINT_NO_FILL_ZERO);
    if tmp_buf.is_null() {
        res = TEE_ERROR_OUT_OF_MEMORY;
        return handle_result(res, tmp_buf);
    }

    let mut tmp_len_var: u32 = tmp_len as u32;
    let mut prop_type = UserTaPropType::USER_TA_PROP_TYPE_INVALID;
    res = unsafe {
        propget_get_property(
            propset_or_enumerator,
            name,
            &mut prop_type,
            tmp_buf,
            &mut tmp_len_var,
        )
    };
    if res != TEE_SUCCESS {
        if res == TEE_ERROR_SHORT_BUFFER {
            if prop_type == UserTaPropType::USER_TA_PROP_TYPE_BINARY_BLOCK {
                unsafe { *value_len = base64_enc_len(tmp_len_var as usize) };
            } else {
                unsafe { *value_len = tmp_len_var as usize };
            }
        }
        return handle_result(res, tmp_buf);
    }

    let l = match prop_type {
        UserTaPropType::USER_TA_PROP_TYPE_BOOL => {
            let bool_value = unsafe { *(tmp_buf as *const bool) };
            let s = if bool_value { "true" } else { "false" };
            copy_string(s, value as _).unwrap()
        }
        UserTaPropType::USER_TA_PROP_TYPE_U32 => {
            let u32_value = unsafe { *(tmp_buf as *const u32) };
            copy_string(&format!("{}", u32_value), value as _).unwrap()
        }
        UserTaPropType::USER_TA_PROP_TYPE_UUID => {
            let uuid_value = unsafe { *(tmp_buf as *const TEE_UUID) };
            copy_string(&format!("{}", uuid_value), value as _).unwrap()
        }
        UserTaPropType::USER_TA_PROP_TYPE_IDENTITY => {
            let identity_value = unsafe { *(tmp_buf as *const TEE_Identity) };
            copy_string(
                &format!("{}:{}", identity_value.login, identity_value.uuid),
                value as _,
            )
            .unwrap()
        }
        UserTaPropType::USER_TA_PROP_TYPE_STRING => {
            let string_value = tmp_buf as *const c_char;
            let c_string_value = unsafe { CStr::from_ptr(string_value).to_str().unwrap() };

            copy_string(c_string_value, value as _).unwrap()
        }
        UserTaPropType::USER_TA_PROP_TYPE_BINARY_BLOCK => {
            let mut tmp_l = unsafe { *value_len };
            if unsafe {
                !base64_enc(tmp_buf as *mut c_char, tmp_len, value as _, &mut tmp_l)
                    && tmp_l <= *value_len
            } {
                res = TEE_ERROR_GENERIC;
                return handle_result(res, tmp_buf);
            }
            tmp_l
        }
        _ => {
            res = TEE_ERROR_BAD_FORMAT;
            return handle_result(res, tmp_buf);
        }
    };

    if l > unsafe { *value_len } {
        res = TEE_ERROR_SHORT_BUFFER;
    }
    unsafe { *value_len = l };
    handle_result(res, tmp_buf)
}

#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetPropertyAsBool(
    propset_or_enumerator: TEE_PropSetHandle,
    name: *const c_char,
    value: *mut bool,
) -> TEE_Result {
    let mut prop_type = UserTaPropType::USER_TA_PROP_TYPE_BOOL;
    let mut bool_len = mem::size_of::<bool>() as u32;
    let mut res = unsafe {
        propget_get_property(
            propset_or_enumerator,
            name,
            &mut prop_type,
            value as *mut c_void,
            &mut bool_len,
        )
    };
    if prop_type != UserTaPropType::USER_TA_PROP_TYPE_BOOL {
        res = TEE_ERROR_BAD_FORMAT;
    }
    if res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND && res != TEE_ERROR_BAD_FORMAT {
        TEE_Panic(0);
    }
    res
}

#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetPropertyAsU32(
    propset_or_enumerator: TEE_PropSetHandle,
    name: *const c_char,
    value: *mut u32,
) -> TEE_Result {
    let mut prop_type = UserTaPropType::USER_TA_PROP_TYPE_U32;
    let mut u32_len = mem::size_of::<u32>() as u32;
    let mut res = unsafe {
        propget_get_property(
            propset_or_enumerator,
            name,
            &mut prop_type,
            value as *mut c_void,
            &mut u32_len,
        )
    };
    if prop_type != UserTaPropType::USER_TA_PROP_TYPE_U32 {
        res = TEE_ERROR_BAD_FORMAT;
    }
    if res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND && res != TEE_ERROR_BAD_FORMAT {
        TEE_Panic(0);
    }
    res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetPropertyAsU64(
    propset_or_enumerator: TEE_PropSetHandle,
    name: *const c_char,
    value: *mut u64,
) -> TEE_Result {
    let mut prop_type = UserTaPropType::USER_TA_PROP_TYPE_U64;
    let mut u64_len = mem::size_of::<u64>() as u32;
    let mut res = unsafe {
        propget_get_property(
            propset_or_enumerator,
            name,
            &mut prop_type,
            value as *mut c_void,
            &mut u64_len,
        )
    };
    if prop_type != UserTaPropType::USER_TA_PROP_TYPE_U64 {
        res = TEE_ERROR_BAD_FORMAT;
    }
    if res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND && res != TEE_ERROR_BAD_FORMAT {
        TEE_Panic(0);
    }
    res
}

#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetPropertyAsBinaryBlock(
    propset_or_enumerator: TEE_PropSetHandle,
    name: *const c_char,
    value: *mut c_void,
    value_len: *mut usize,
) -> TEE_Result {
    let mut prop_type = UserTaPropType::USER_TA_PROP_TYPE_BINARY_BLOCK;
    let mut tmp_len = unsafe { *value_len } as u32;
    let mut res = unsafe {
        propget_get_property(
            propset_or_enumerator,
            name,
            &mut prop_type,
            value,
            &mut tmp_len,
        )
    };
    if prop_type != UserTaPropType::USER_TA_PROP_TYPE_BINARY_BLOCK {
        res = TEE_ERROR_BAD_FORMAT;
    }
    if res != TEE_SUCCESS
        && res != TEE_ERROR_ITEM_NOT_FOUND
        && res != TEE_ERROR_BAD_FORMAT
        && res != TEE_ERROR_SHORT_BUFFER
    {
        TEE_Panic(0);
    }
    unsafe { *value_len = tmp_len as usize };
    res
}

#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetPropertyAsUUID(
    propset_or_enumerator: TEE_PropSetHandle,
    name: *const c_char,
    value: *mut TEE_UUID,
) -> TEE_Result {
    let mut prop_type = UserTaPropType::USER_TA_PROP_TYPE_UUID;
    let mut uuid_len = mem::size_of::<TEE_UUID>() as u32;
    let mut res = unsafe {
        propget_get_property(
            propset_or_enumerator,
            name,
            &mut prop_type,
            value as *mut c_void,
            &mut uuid_len,
        )
    };
    if prop_type != UserTaPropType::USER_TA_PROP_TYPE_UUID {
        res = TEE_ERROR_BAD_FORMAT;
    }
    if res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND && res != TEE_ERROR_BAD_FORMAT {
        TEE_Panic(0);
    }
    res
}

#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetPropertyAsIdentity(
    propset_or_enumerator: TEE_PropSetHandle,
    name: *const c_char,
    value: *mut TEE_Identity,
) -> TEE_Result {
    let mut prop_type = UserTaPropType::USER_TA_PROP_TYPE_IDENTITY;
    let mut identity_len = mem::size_of::<TEE_Identity>() as u32;
    let mut res = unsafe {
        propget_get_property(
            propset_or_enumerator,
            name,
            &mut prop_type,
            value as *mut c_void,
            &mut identity_len,
        )
    };
    if prop_type != UserTaPropType::USER_TA_PROP_TYPE_IDENTITY {
        res = TEE_ERROR_BAD_FORMAT;
    }
    if res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND && res != TEE_ERROR_BAD_FORMAT {
        TEE_Panic(0);
    }
    res
}

#[unsafe(no_mangle)]
pub extern "C" fn TEE_AllocatePropertyEnumerator(enumerator: *mut TEE_PropSetHandle) -> TEE_Result {
    let pe = TEE_Malloc(
        mem::size_of::<PropEnumerator>() as usize,
        TEE_USER_MEM_HINT_NO_FILL_ZERO,
    );
    if pe.is_null() {
        return TEE_ERROR_OUT_OF_MEMORY;
    }
    unsafe { ptr::write(enumerator, pe as TEE_PropSetHandle) };
    unsafe { TEE_ResetPropertyEnumerator(*enumerator) };
    TEE_SUCCESS
}

#[unsafe(no_mangle)]
pub extern "C" fn TEE_FreePropertyEnumerator(enumerator: TEE_PropSetHandle) {
    let pe = enumerator as *mut PropEnumerator;
    TEE_Free(pe as *mut c_void);
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_StartPropertyEnumerator(
    enumerator: TEE_PropSetHandle,
    prop_set: TEE_PropSetHandle,
) {
    let pe = enumerator as *mut PropEnumerator;
    if pe.is_null() {
        return;
    }
    unsafe { (*pe).idx = 0 };
    unsafe { (*pe).prop_set = prop_set };
}

#[unsafe(no_mangle)]
pub extern "C" fn TEE_ResetPropertyEnumerator(enumerator: TEE_PropSetHandle) {
    let pe = enumerator as *mut PropEnumerator;
    unsafe { (*pe).idx = PROP_ENUMERATOR_NOT_STARTED };
}

#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetPropertyName(
    enumerator: TEE_PropSetHandle,
    name: *mut c_void,
    name_len: *mut usize,
) -> TEE_Result {
    let pe = enumerator as *mut PropEnumerator;
    let tmp_name_len = name_len;
    let mut eps: *const UserTaProperty = core::ptr::null();
    let mut eps_len: usize = 0;
    if pe.is_null() {
        TEE_Panic(0);
    }
    let mut res = unsafe { propset_get((*pe).prop_set, &mut eps, &mut eps_len) };
    if res != TEE_SUCCESS {
        if res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND && res != TEE_ERROR_SHORT_BUFFER {
            TEE_Panic(0);
        }
        return res;
    }
    if unsafe { (*pe).idx } < (eps_len as u32) {
        let str = unsafe { &*eps.offset((*pe).idx as isize) }.name;
        let buffer_len = copy_constu8_string(str as _, name as *mut u8).unwrap() + 1;
        if buffer_len > unsafe { *tmp_name_len } {
            res = TEE_ERROR_SHORT_BUFFER;
        }
        unsafe { *tmp_name_len = buffer_len as usize };
    } else {
        res = unsafe {
            _utee_get_property(
                (*pe).prop_set as u64,
                ((*pe).idx - eps_len as u32) as u64,
                name,
                tmp_name_len as *mut u32,
                null_mut(),
                null_mut(),
                null_mut(),
            )
        } as u32;
    }
    if res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND && res != TEE_ERROR_SHORT_BUFFER {
        TEE_Panic(0);
    }
    res
}
#[unsafe(no_mangle)]
pub extern "C" fn TEE_GetNextProperty(enumerator: TEE_PropSetHandle) -> TEE_Result {
    let pe = enumerator as *mut PropEnumerator;
    let mut eps: *const UserTaProperty = core::ptr::null();
    let mut eps_len: usize = 0;
    if pe.is_null() {
        TEE_Panic(0);
    }
    if unsafe { (*pe).idx } == PROP_ENUMERATOR_NOT_STARTED {
        return TEE_ERROR_ITEM_NOT_FOUND;
    }
    let mut res = unsafe { propset_get((*pe).prop_set, &mut eps, &mut eps_len) };
    if res != TEE_SUCCESS {
        if res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND {
            TEE_Panic(0);
        }
        return res;
    }
    let next_idx = unsafe { (*pe).idx + 1 };
    unsafe { (*pe).idx = next_idx };
    if next_idx < (eps_len as u32) {
        res = TEE_SUCCESS;
    } else {
        res = unsafe {
            _utee_get_property(
                (*pe).prop_set as u64,
                (next_idx - eps_len as u32) as u64,
                null_mut(),
                null_mut(),
                null_mut(),
                null_mut(),
                null_mut(),
            )
        } as u32;
    }
    if res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND {
        TEE_Panic(0);
    }
    res
}