blob: 14d3b46a71065f77f2d74a8771b2cac570aa7dbe [file] [log] [blame]
b.liub17525e2025-05-14 17:22:29 +08001#include <dlfcn.h>
2#include <stdio.h>
3#include <string.h>
4#include <stdint.h>
5
6#include "ql_tee_service.h"
7#include "mbtk_log.h"
8#include <tee_client_api.h>
9
10
11struct test_ctx {
12 TEEC_Context ctx;
13 TEEC_Session sess;
14};
15
16
17struct test_ctx ctx;
18
19const void *obj_id;
20uint32_t obj_size;
21
22#define lib_secure_path "/lib/libsecure_storage.so"
23static void *dlHandle_secure;
24
25
26
27
28int (*prepare_tee_session)(struct test_ctx *ctx);
29void (*terminate_tee_session)(struct test_ctx *ctx);
30TEEC_Result (*read_secure_object)(struct test_ctx *ctx, const void *id, uint32_t id_size, char *data, size_t data_len);
31TEEC_Result (*write_secure_object)(struct test_ctx *ctx, const void *id, uint32_t id_size, char *data, size_t data_len);
32TEEC_Result (*delete_secure_object)(struct test_ctx *ctx, const void *id, uint32_t id_size);
33
34
35static int tee_api_import(void)
36{
37 dlHandle_secure = dlopen(lib_secure_path, RTLD_NOW);
38 if (dlHandle_secure == NULL)
39 {
40 return -1;
41 }
42
43 prepare_tee_session = (int (*)(struct test_ctx *ctx))dlsym(dlHandle_secure, "prepare_tee_session");
44 if (prepare_tee_session == NULL)
45 {
46 LOGE("prepare_tee_session dlsym fail\n");
47 return -1;
48 }
49
50 terminate_tee_session = (void (*)(struct test_ctx *ctx))dlsym(dlHandle_secure, "terminate_tee_session");
51 if (terminate_tee_session == NULL)
52 {
53 LOGE("terminate_tee_session dlsym fail\n");
54 return -1;
55 }
56
57 read_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const void *id, uint32_t id_size,char *data, size_t data_len))dlsym(dlHandle_secure, "read_secure_object");
58 if (read_secure_object == NULL)
59 {
60 LOGE("read_secure_object dlsym fail\n");
61 return -1;
62 }
63
64 write_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const void *id, uint32_t id_size, char *data, size_t data_len))dlsym(dlHandle_secure, "write_secure_object");
65 if (write_secure_object == NULL)
66 {
67 LOGE("write_secure_object dlsym fail\n");
68 return -1;
69 }
70
71 delete_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const void *id, uint32_t id_size))dlsym(dlHandle_secure, "delete_secure_object");
72 if (delete_secure_object == NULL)
73 {
74 LOGE("delete_secure_object dlsym fail\n");
75 return -1;
76 }
77
78 return 0;
79}
80
81/**
82* @brief init tee sdk
83* @param [in] None
84* @param [out] None
85* @retval GSW_HAL_SUCCESS is success\other is fail
86*/
87ql_tee_error_t ql_ss_initialize(void)
88{
89 int32_t ret = 0;
90 ret = tee_api_import();
91 if(ret)
92 {
93 LOGE("tee_api_import fail\n");
94 return ret;
95 }
96 ret = prepare_tee_session(&ctx);
97
98 return ret;
99}
100
101void ql_ss_deinitialize(void)
102{
103
104 terminate_tee_session(&ctx);
105
106}
107ql_tee_error_t ql_ss_open(const void *id, uint32_t id_size, uint32_t *object)
108{
109
110 obj_id = id;
111 obj_size = id_size;
112 return 0;
113}
114
115ql_tee_error_t ql_ss_close(uint32_t object)
116{
117
118 obj_id = NULL;
119 obj_size = 0;
120 return 0;
121
122}
123
124/**
125* @brief read sensitive data from tee
126* @param [in] char* in_obj_name :Sensitive data name
127* @param [in] unsigned int* p_out_buf_len:The size of sensitive data output cache
128* @param [out] char* out_buf:Cache of sensitive data output
129* @param [out] unsigned int* p_out_buf_len:Sensitive data length
130* @retval GSW_HAL_SUCCESS is success\other is fail
131*/
132
133ql_tee_error_t ql_ss_read(uint32_t object, void *data, uint32_t data_size, uint32_t *count)
134{
135 int32_t ret = 0;
136 TEEC_Result res;
137
138 res = read_secure_object(&ctx, obj_id, obj_size, data, data_size);
139 if (res != TEEC_SUCCESS)
140 {
141 LOGE("Failed to read an object from the secure storage");
142 ret = -1;
143 }
144
145 *count = strlen(data);
146 return ret;
147}
148
149
150/**
151* @brief write sensitive data to tee
152* @param [in] char* in_obj_name :Sensitive data name
153* @param [in] char* in_buf:A cache for writing sensitive data
154* @param [out] unsigned int in_buf_len:Sensitive data length
155* @retval GSW_HAL_SUCCESS is success\other is fail
156*/
157ql_tee_error_t ql_ss_write(uint32_t object, void *data, uint32_t data_size)
158{
159 int32_t ret = 0;
160 TEEC_Result res;
161 res = write_secure_object(&ctx, obj_id, obj_size, data, data_size);
162 if (res != TEEC_SUCCESS)
163 {
164 LOGE("Failed to write an object from the secure storage");
165 ret = -1;
166 }
167
168 return ret;
169}
170
171
172/**
173* @brief delete sensitive data from tee
174* @param [in] char* in_obj_name :Sensitive data name
175* @retval GSW_HAL_SUCCESS is success\other is fail
176*/
177ql_tee_error_t ql_ss_unlink(uint32_t object)
178{
179 int32_t ret = 0;
180 TEEC_Result res;
181 res = delete_secure_object(&ctx, obj_id, obj_size);
182 if (res != TEEC_SUCCESS)
183 {
184 LOGE("Failed to delete the object: 0x%x", res);
185 ret = -1;
186 }
187
188
189 return ret;
190
191}
192
193
194
195