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