blob: 2ba8ece1769a8d45f2273d2535c432a8e3252db7 [file] [log] [blame]
b.liu68a94c92025-05-24 12:53:41 +08001
2#include "gsw/gsw_tee.h"
3#include <dlfcn.h>
4#include <stdio.h>
5#include <string.h>
6#include <stdint.h>
7
8
9#include <tee_client_api.h>
10
11#ifndef LOG_ERR_LEVEL
12#define LOG_ERR_LEVEL 3 /* error conditions */
13#endif
14#ifndef LOG_WARN_LEVEL
15#define LOG_WARN_LEVEL 4 /* warning conditions */
16#endif
17#ifndef LOG_INFO_LEVEL
18#define LOG_INFO_LEVEL 6 /* informational */
19#endif
20#ifndef LOG_DEBUG_LEVEL
21#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
22#endif
23#ifndef LOG_VERBOSE_LEVEL
24#define LOG_VERBOSE_LEVEL 8
25#endif
26
27#define LOGV(fmt, args ...) \
28 do{ \
29 char *file_ptr_1001 = __FILE__; \
30 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
31 char line_1001[10] = {0}; \
32 sprintf(line_1001, "%d", __LINE__); \
33 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
34 if(*ptr_1001 == '/') \
35 break; \
36 ptr_1001--; \
37 } \
38 mbtk_log(LOG_VERBOSE_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
39 } while(0)
40
41#define LOGI(fmt, args...) \
42 do{ \
43 char *file_ptr_1001 = __FILE__; \
44 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
45 char line_1001[10] = {0}; \
46 sprintf(line_1001, "%d", __LINE__); \
47 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
48 if(*ptr_1001 == '/') \
49 break; \
50 ptr_1001--; \
51 } \
52 mbtk_log(LOG_INFO_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
53 } while(0)
54
55#define LOGD(fmt, args...) \
56 do{ \
57 char *file_ptr_1001 = __FILE__; \
58 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
59 char line_1001[10] = {0}; \
60 sprintf(line_1001, "%d", __LINE__); \
61 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
62 if(*ptr_1001 == '/') \
63 break; \
64 ptr_1001--; \
65 } \
66 mbtk_log(LOG_DEBUG_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
67 } while(0)
68
69#define LOGW(fmt, args...) \
70 do{ \
71 char *file_ptr_1001 = __FILE__; \
72 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
73 char line_1001[10] = {0}; \
74 sprintf(line_1001, "%d", __LINE__); \
75 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
76 if(*ptr_1001 == '/') \
77 break; \
78 ptr_1001--; \
79 } \
80 mbtk_log(LOG_WARN_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
81 } while(0)
82
83#define LOGE(fmt, args...) \
84 do{ \
85 char *file_ptr_1001 = __FILE__; \
86 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
87 char line_1001[10] = {0}; \
88 sprintf(line_1001, "%d", __LINE__); \
89 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
90 if(*ptr_1001 == '/') \
91 break; \
92 ptr_1001--; \
93 } \
94 mbtk_log(LOG_ERR_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
95 } while(0)
96
97struct test_ctx {
98 TEEC_Context ctx;
99 TEEC_Session sess;
100};
101
102
103struct test_ctx ctx;
104
105#define lib_secure_path "/lib/libsecure_storage.so"
106static void *dlHandle_secure;
107
108#define lib_mbtk_path "/lib/libmbtk_lib.so"
109static void *dlHandle_mbtk;
110
111
112
113
114
115static void (*mbtk_log)(int level, const char *format, ...);
116static void (*mbtk_log_init)(char *path, char *tag);
117
118
119
120int (*prepare_tee_session)(struct test_ctx *ctx);
121void (*terminate_tee_session)(struct test_ctx *ctx);
122TEEC_Result (*read_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t data_len);
123TEEC_Result (*write_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t data_len);
124TEEC_Result (*delete_secure_object)(struct test_ctx *ctx, const char *id);
125
126
127static int tee_api_import(void)
128{
129
130 dlHandle_mbtk = dlopen(lib_mbtk_path, RTLD_NOW);
131 if (dlHandle_mbtk == NULL)
132 {
133 return GSW_HAL_FAIL;
134 }
135
136 dlHandle_secure = dlopen(lib_secure_path, RTLD_NOW);
137 if (dlHandle_secure == NULL)
138 {
139 return GSW_HAL_FAIL;
140 }
141
142 mbtk_log_init = (void (*)(char *path, char *tag))dlsym(dlHandle_mbtk, "mbtk_log_init");
143 if (mbtk_log_init == NULL)
144 {
145 return GSW_HAL_FAIL;
146 }
147
148 mbtk_log = (void (*)(int level, const char *format, ...))dlsym(dlHandle_mbtk, "mbtk_log");
149 if (mbtk_log == NULL)
150 {
151 return GSW_HAL_FAIL;
152 }
153
154 prepare_tee_session = (int (*)(struct test_ctx *ctx))dlsym(dlHandle_secure, "prepare_tee_session");
155 if (prepare_tee_session == NULL)
156 {
157 LOGE("prepare_tee_session dlsym fail\n");
158 return GSW_HAL_FAIL;
159 }
160
161 terminate_tee_session = (void (*)(struct test_ctx *ctx))dlsym(dlHandle_secure, "terminate_tee_session");
162 if (terminate_tee_session == NULL)
163 {
164 LOGE("terminate_tee_session dlsym fail\n");
165 return GSW_HAL_FAIL;
166 }
167
168 read_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id,char *data, size_t data_len))dlsym(dlHandle_secure, "read_secure_object");
169 if (read_secure_object == NULL)
170 {
171 LOGE("read_secure_object dlsym fail\n");
172 return GSW_HAL_FAIL;
173 }
174
175 write_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id,char *data, size_t data_len))dlsym(dlHandle_secure, "write_secure_object");
176 if (write_secure_object == NULL)
177 {
178 LOGE("write_secure_object dlsym fail\n");
179 return GSW_HAL_FAIL;
180 }
181
182 delete_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id))dlsym(dlHandle_secure, "delete_secure_object");
183 if (delete_secure_object == NULL)
184 {
185 LOGE("delete_secure_object dlsym fail\n");
186 return GSW_HAL_FAIL;
187 }
188
189 return GSW_HAL_SUCCESS;
190}
191
192/**
193* @brief init tee sdk
194* @param [in] None
195* @param [out] None
196* @retval GSW_HAL_SUCCESS is success\other is fail
197*/
198int32_t gsw_tee_sdk_init(void)
199{
200 int32_t ret = 0;
201 ret = tee_api_import();
202 if(ret)
203 {
204 LOGE("tee_api_import fail\n");
205 return ret;
206 }
207 ret = prepare_tee_session(&ctx);
208
209 return ret;
210}
211
212
213/**
214* @brief read sensitive data from tee
215* @param [in] char* in_obj_name :Sensitive data name
216* @param [in] unsigned int* p_out_buf_len:The size of sensitive data output cache
217* @param [out] char* out_buf:Cache of sensitive data output
218* @param [out] unsigned int* p_out_buf_len:Sensitive data length
219* @retval GSW_HAL_SUCCESS is success\other is fail
220*/
221#define basic_buf_len 7000
222int32_t gsw_tee_read_secure_data(const char* in_obj_name, char* out_buf, unsigned int* p_out_buf_len)
223{
224 int32_t ret = 0;
225 TEEC_Result res;
226 res = read_secure_object(&ctx, in_obj_name, out_buf, basic_buf_len);
227 if (res != TEEC_SUCCESS)
228 {
229 LOGE("Failed to read an object from the secure storage");
230 ret = -1;
231 }
232
233 *p_out_buf_len = strlen(out_buf);
234 return ret;
235}
236
237
238/**
239* @brief write sensitive data to tee
240* @param [in] char* in_obj_name :Sensitive data name
241* @param [in] char* in_buf:A cache for writing sensitive data
242* @param [out] unsigned int in_buf_len:Sensitive data length
243* @retval GSW_HAL_SUCCESS is success\other is fail
244*/
245int32_t gsw_tee_write_secure_data(const char* in_obj_name, char* in_buf, unsigned int in_buf_len)
246{
247 int32_t ret = 0;
248 TEEC_Result res;
249 res = write_secure_object(&ctx, in_obj_name,in_buf, in_buf_len);
250 if (res != TEEC_SUCCESS)
251 {
252 LOGE("Failed to write an object from the secure storage");
253 ret = -1;
254 }
255
256 return ret;
257}
258
259
260/**
261* @brief delete sensitive data from tee
262* @param [in] char* in_obj_name :Sensitive data name
263* @retval GSW_HAL_SUCCESS is success\other is fail
264*/
265int32_t gsw_tee_delete_secure_data(const char* in_obj_name)
266{
267 int32_t ret = 0;
268 TEEC_Result res;
269 res = delete_secure_object(&ctx, in_obj_name);
270 if (res != TEEC_SUCCESS)
271 {
272 LOGE("Failed to delete the object: 0x%x", res);
273 ret = -1;
274 }
275
276
277 return ret;
278
279}
280
281/**
282* @brief check sensitive data from tee
283* @param [in] char* in_obj_name :Sensitive data name
284* @retval GSW_HAL_SUCCESS is exist\ other is not exist or fail
285*/
286int32_t gsw_tee_check_secure_data(const char* in_obj_name)
287{
288 int32_t ret = 1;
289 TEEC_Result res;
290 char out_buf[4];
291 res = read_secure_object(&ctx, in_obj_name, out_buf, sizeof(out_buf));
292
293 if (res == TEEC_ERROR_ITEM_NOT_FOUND)
294 {
295 LOGE("the obj no found\n");
296 ret = GSW_HAL_NOFOUND;
297 }
298 else if (res == TEEC_SUCCESS)
299 {
300 LOGE("the obj is exist\n");
301 ret = GSW_HAL_SUCCESS;
302 }
303 else
304 {
305 LOGE("Failed to read an object from the secure storage");
306 ret = GSW_HAL_FAIL;
307 }
308
309 return ret;
310}
311
312int32_t gsw_tee_sdk_deinit(void)
313{
314 if (terminate_tee_session) {
315 terminate_tee_session(&ctx); // 终止TEE会话
316 terminate_tee_session = NULL;
317 }
318
319 if (dlHandle_secure) {
320 dlclose(dlHandle_secure); // 卸载安全库
321 dlHandle_secure = NULL;
322 }
323
324 if (dlHandle_mbtk) {
325 dlclose(dlHandle_mbtk); // 卸载日志库
326 dlHandle_mbtk = NULL;
327 }
328
329 return GSW_HAL_SUCCESS;
330}
331