b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 1 | #include <dlfcn.h> |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 2 | #include <stdbool.h> |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 3 | #include <stdio.h> |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 4 | #include <stdlib.h> |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 5 | #include <string.h> |
| 6 | #include <stdint.h> |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 7 | #include "gsw_secrypt_ss_interface.h" |
| 8 | #include "gsw_hal_errcode.h" |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 9 | #include "gsw_log_interface.h" |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 10 | |
l.yang | 6a42e4d | 2025-05-28 01:04:20 -0700 | [diff] [blame] | 11 | #define GSW_TEE "[HAL][GSW_TEE]" |
| 12 | |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 13 | /** |
| 14 | * struct TEEC_Context - Represents a connection between a client application |
| 15 | * and a TEE. |
| 16 | */ |
| 17 | typedef struct { |
| 18 | /* Implementation defined */ |
| 19 | struct { |
| 20 | int fd; |
| 21 | bool reg_mem; |
| 22 | bool memref_null; |
| 23 | } imp; |
| 24 | } TEEC_Context; |
| 25 | |
| 26 | /** |
| 27 | * struct TEEC_Session - Represents a connection between a client application |
| 28 | * and a trusted application. |
| 29 | */ |
| 30 | typedef struct { |
| 31 | /* Implementation defined */ |
| 32 | struct { |
| 33 | TEEC_Context *ctx; |
| 34 | uint32_t session_id; |
| 35 | } imp; |
| 36 | } TEEC_Session; |
| 37 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 38 | struct test_ctx { |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 39 | TEEC_Context ctx; |
| 40 | TEEC_Session sess; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 41 | }; |
| 42 | |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 43 | #define TEEC_SUCCESS 0x00000000 |
| 44 | #define TEEC_ERROR_STORAGE_NOT_AVAILABLE 0xF0100003 |
| 45 | #define TEEC_ERROR_GENERIC 0xFFFF0000 |
| 46 | #define TEEC_ERROR_ACCESS_DENIED 0xFFFF0001 |
| 47 | #define TEEC_ERROR_CANCEL 0xFFFF0002 |
| 48 | #define TEEC_ERROR_ACCESS_CONFLICT 0xFFFF0003 |
| 49 | #define TEEC_ERROR_EXCESS_DATA 0xFFFF0004 |
| 50 | #define TEEC_ERROR_BAD_FORMAT 0xFFFF0005 |
| 51 | #define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006 |
| 52 | #define TEEC_ERROR_BAD_STATE 0xFFFF0007 |
| 53 | #define TEEC_ERROR_ITEM_NOT_FOUND 0xFFFF0008 |
| 54 | #define TEEC_ERROR_NOT_IMPLEMENTED 0xFFFF0009 |
| 55 | #define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A |
| 56 | #define TEEC_ERROR_NO_DATA 0xFFFF000B |
| 57 | #define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C |
| 58 | #define TEEC_ERROR_BUSY 0xFFFF000D |
| 59 | #define TEEC_ERROR_COMMUNICATION 0xFFFF000E |
| 60 | #define TEEC_ERROR_SECURITY 0xFFFF000F |
| 61 | #define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010 |
| 62 | #define TEEC_ERROR_EXTERNAL_CANCEL 0xFFFF0011 |
| 63 | #define TEEC_ERROR_TARGET_DEAD 0xFFFF3024 |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 64 | |
| 65 | struct test_ctx ctx; |
| 66 | |
| 67 | #define lib_secure_path "/lib/libsecure_storage.so" |
| 68 | static void *dlHandle_secure; |
| 69 | |
| 70 | #define lib_mbtk_path "/lib/libmbtk_lib.so" |
| 71 | static void *dlHandle_mbtk; |
lichengzhang | 36264d6 | 2025-07-30 15:55:35 +0800 | [diff] [blame] | 72 | #define PUBLIC_KEY_SEC_NAME "public_key_obj" |
lichengzhang | 6965ca5 | 2025-08-08 11:28:15 +0800 | [diff] [blame^] | 73 | #define SEC_LEVEL1_SEC_NAME "sec_level1_obj" |
lichengzhang | 36264d6 | 2025-07-30 15:55:35 +0800 | [diff] [blame] | 74 | typedef struct |
| 75 | { |
| 76 | size_t length; |
| 77 | int is_update; |
| 78 | //int is_read; |
| 79 | unsigned char *content; |
| 80 | } tmp_sec; |
| 81 | tmp_sec public_key_sec = {0}; |
| 82 | tmp_sec sec_level1_sec = {0}; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 83 | |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 84 | typedef uint32_t TEEC_Result; |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 85 | // static void (*mbtk_log)(int level, const char *format, ...); |
| 86 | // static void (*mbtk_log_init)(char *path, char *tag); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 87 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 88 | int (*prepare_tee_session)(struct test_ctx *ctx); |
| 89 | void (*terminate_tee_session)(struct test_ctx *ctx); |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 90 | TEEC_Result (*read_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t *data_len); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 91 | TEEC_Result (*write_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t data_len); |
| 92 | TEEC_Result (*delete_secure_object)(struct test_ctx *ctx, const char *id); |
| 93 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 94 | static int tee_api_import(void) |
| 95 | { |
| 96 | |
| 97 | dlHandle_mbtk = dlopen(lib_mbtk_path, RTLD_NOW); |
| 98 | if (dlHandle_mbtk == NULL) |
| 99 | { |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 100 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | dlHandle_secure = dlopen(lib_secure_path, RTLD_NOW); |
| 104 | if (dlHandle_secure == NULL) |
| 105 | { |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 106 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 107 | } |
| 108 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 109 | // mbtk_log_init = (void (*)(char *path, char *tag))dlsym(dlHandle_mbtk, "mbtk_log_init"); |
| 110 | // if (mbtk_log_init == NULL) |
| 111 | // { |
| 112 | // return GSW_HAL_NORMAL_FAIL; |
| 113 | // } |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 114 | |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 115 | // mbtk_log = (void (*)(int level, const char *format, ...))dlsym(dlHandle_mbtk, "mbtk_log"); |
| 116 | // if (mbtk_log == NULL) |
| 117 | // { |
| 118 | // return GSW_HAL_NORMAL_FAIL; |
| 119 | // } |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 120 | |
| 121 | prepare_tee_session = (int (*)(struct test_ctx *ctx))dlsym(dlHandle_secure, "prepare_tee_session"); |
| 122 | if (prepare_tee_session == NULL) |
| 123 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 124 | LOGE(GSW_TEE,"prepare_tee_session dlsym fail\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 125 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | terminate_tee_session = (void (*)(struct test_ctx *ctx))dlsym(dlHandle_secure, "terminate_tee_session"); |
| 129 | if (terminate_tee_session == NULL) |
| 130 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 131 | LOGE(GSW_TEE,"terminate_tee_session dlsym fail\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 132 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 133 | } |
| 134 | |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 135 | read_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id,char *data, size_t *data_len))dlsym(dlHandle_secure, "read_secure_object"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 136 | if (read_secure_object == NULL) |
| 137 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 138 | LOGE(GSW_TEE,"read_secure_object dlsym fail\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 139 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | write_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id,char *data, size_t data_len))dlsym(dlHandle_secure, "write_secure_object"); |
| 143 | if (write_secure_object == NULL) |
| 144 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 145 | LOGE(GSW_TEE,"write_secure_object dlsym fail\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 146 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | delete_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id))dlsym(dlHandle_secure, "delete_secure_object"); |
| 150 | if (delete_secure_object == NULL) |
| 151 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 152 | LOGE(GSW_TEE,"delete_secure_object dlsym fail\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 153 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | return GSW_HAL_SUCCESS; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @brief init tee sdk |
| 161 | * @param [in] None |
| 162 | * @param [out] None |
| 163 | * @retval GSW_HAL_SUCCESS is success\other is fail |
| 164 | */ |
| 165 | int32_t gsw_tee_sdk_init(void) |
| 166 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 167 | LOGE(GSW_TEE,"init start\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 168 | int32_t ret = 0; |
| 169 | ret = tee_api_import(); |
| 170 | if(ret) |
| 171 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 172 | LOGE(GSW_TEE,"tee_api_import fail\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 173 | return ret; |
| 174 | } |
| 175 | ret = prepare_tee_session(&ctx); |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 176 | LOGE(GSW_TEE,"init end\n"); |
lichengzhang | 36264d6 | 2025-07-30 15:55:35 +0800 | [diff] [blame] | 177 | if (public_key_sec.content == NULL) { |
| 178 | public_key_sec.content = malloc(7000); |
| 179 | public_key_sec.length = 0; |
| 180 | public_key_sec.is_update = 0; |
| 181 | } |
| 182 | |
| 183 | if (sec_level1_sec.content == NULL) { |
| 184 | sec_level1_sec.content = malloc(7000); |
| 185 | sec_level1_sec.length = 0; |
| 186 | sec_level1_sec.is_update = 0; |
| 187 | } |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 188 | return ret; |
| 189 | } |
| 190 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 191 | /** |
| 192 | * @brief read sensitive data from tee |
| 193 | * @param [in] char* in_obj_name :Sensitive data name |
| 194 | * @param [in] unsigned int* p_out_buf_len:The size of sensitive data output cache |
| 195 | * @param [out] char* out_buf:Cache of sensitive data output |
| 196 | * @param [out] unsigned int* p_out_buf_len:Sensitive data length |
| 197 | * @retval GSW_HAL_SUCCESS is success\other is fail |
| 198 | */ |
| 199 | #define basic_buf_len 7000 |
| 200 | int32_t gsw_tee_read_secure_data(const char* in_obj_name, char* out_buf, unsigned int* p_out_buf_len) |
| 201 | { |
lichengzhang | 36264d6 | 2025-07-30 15:55:35 +0800 | [diff] [blame] | 202 | //LOGE(GSW_TEE,"start read\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 203 | if (in_obj_name == NULL || out_buf == NULL) |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 204 | { |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 205 | return GSW_HAL_NORMAL_FAIL; |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 206 | } |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 207 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 208 | int32_t ret = 0; |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 209 | size_t size = basic_buf_len; |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 210 | char *tmp_buf = (char*)malloc(basic_buf_len); |
| 211 | if (NULL == tmp_buf) |
| 212 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 213 | LOGE(GSW_TEE,"Failed malloc fail"); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 214 | return GSW_HAL_NO_MEMORY; |
| 215 | } |
lichengzhang | 36264d6 | 2025-07-30 15:55:35 +0800 | [diff] [blame] | 216 | |
| 217 | bool is_pubkey = !strncmp(in_obj_name, PUBLIC_KEY_SEC_NAME, strlen(PUBLIC_KEY_SEC_NAME)); |
| 218 | bool is_level1 = !strncmp(in_obj_name, SEC_LEVEL1_SEC_NAME, strlen(SEC_LEVEL1_SEC_NAME)); |
| 219 | |
| 220 | if (is_pubkey || is_level1) |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 221 | { |
lichengzhang | 36264d6 | 2025-07-30 15:55:35 +0800 | [diff] [blame] | 222 | tmp_sec *target = is_pubkey ? &public_key_sec : &sec_level1_sec; |
| 223 | if (target->is_update) |
| 224 | { |
| 225 | if (target->length == 0 || target->content[0] == '\0') |
| 226 | ret = GSW_HAL_ERROR_TEE_SFS_FILE_NOEXIST; |
| 227 | else |
| 228 | { |
| 229 | memcpy(out_buf, target->content, target->length); |
| 230 | *p_out_buf_len = target->length; |
| 231 | ret = GSW_HAL_SUCCESS; |
| 232 | } |
| 233 | free(tmp_buf); |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | TEEC_Result res = read_secure_object(&ctx, in_obj_name, tmp_buf, &size); |
| 239 | |
| 240 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) |
| 241 | { |
| 242 | LOGE(GSW_TEE, "the obj no found\n"); |
| 243 | ret = GSW_HAL_ERROR_TEE_SFS_FILE_NOEXIST; |
| 244 | |
| 245 | target->is_update = 1; |
| 246 | target->length = 0; |
| 247 | target->content[0] = '\0'; |
| 248 | } |
| 249 | else if (res == TEEC_SUCCESS) |
| 250 | { |
| 251 | LOGE(GSW_TEE, "the obj is exist\n"); |
| 252 | memcpy(target->content, tmp_buf, size); |
| 253 | |
| 254 | target->length = size; |
| 255 | target->is_update = 1; |
| 256 | |
| 257 | memcpy(out_buf, tmp_buf, size); |
| 258 | *p_out_buf_len = size; |
| 259 | |
| 260 | ret = GSW_HAL_SUCCESS; |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | LOGE(GSW_TEE,"Failed to read an object from the secure storage"); |
| 265 | ret = GSW_HAL_NORMAL_FAIL; |
| 266 | } |
| 267 | |
lichengzhang | 55aed35 | 2025-07-11 18:30:08 +0800 | [diff] [blame] | 268 | free(tmp_buf); |
| 269 | return ret; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 270 | } |
lichengzhang | 36264d6 | 2025-07-30 15:55:35 +0800 | [diff] [blame] | 271 | |
| 272 | TEEC_Result res = read_secure_object(&ctx, in_obj_name, tmp_buf, &size); |
| 273 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) |
| 274 | { |
| 275 | LOGE(GSW_TEE,"the obj no found\n"); |
| 276 | ret = GSW_HAL_ERROR_TEE_SFS_FILE_NOEXIST; |
| 277 | } |
| 278 | else if (res == TEEC_SUCCESS) |
| 279 | { |
| 280 | LOGE(GSW_TEE,"the obj is exist\n"); |
| 281 | memcpy(out_buf, tmp_buf, size); |
| 282 | *p_out_buf_len = size; |
| 283 | ret = GSW_HAL_SUCCESS; |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | LOGE(GSW_TEE,"Failed to read an object from the secure storage"); |
| 288 | ret = GSW_HAL_NORMAL_FAIL; |
| 289 | } |
| 290 | |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 291 | free(tmp_buf); |
lichengzhang | 36264d6 | 2025-07-30 15:55:35 +0800 | [diff] [blame] | 292 | return ret; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 293 | } |
| 294 | |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 295 | /** |
| 296 | * @brief write sensitive data to tee |
| 297 | * @param [in] char* in_obj_name :Sensitive data name |
| 298 | * @param [in] char* in_buf:A cache for writing sensitive data |
| 299 | * @param [out] unsigned int in_buf_len:Sensitive data length |
| 300 | * @retval GSW_HAL_SUCCESS is success\other is fail |
| 301 | */ |
| 302 | int32_t gsw_tee_write_secure_data(const char* in_obj_name, char* in_buf, unsigned int in_buf_len) |
| 303 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 304 | LOGE(GSW_TEE,"write start\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 305 | if (in_obj_name == NULL || in_buf == NULL) |
| 306 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 307 | int32_t ret = 0; |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 308 | |
| 309 | TEEC_Result res = write_secure_object(&ctx, in_obj_name,in_buf, in_buf_len); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 310 | if (res != TEEC_SUCCESS) |
| 311 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 312 | LOGE(GSW_TEE,"Failed to write an object from the secure storage"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 313 | ret = GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 314 | } |
lichengzhang | 36264d6 | 2025-07-30 15:55:35 +0800 | [diff] [blame] | 315 | bool is_pubkey = !strncmp(in_obj_name, PUBLIC_KEY_SEC_NAME, strlen(PUBLIC_KEY_SEC_NAME)); |
| 316 | bool is_level1 = !strncmp(in_obj_name, SEC_LEVEL1_SEC_NAME, strlen(SEC_LEVEL1_SEC_NAME)); |
| 317 | if (is_pubkey || is_level1) |
| 318 | { |
| 319 | tmp_sec *target = is_pubkey ? &public_key_sec : &sec_level1_sec; |
| 320 | target->is_update = 0; |
| 321 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 322 | LOGE(GSW_TEE,"write really end\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | /** |
| 328 | * @brief delete sensitive data from tee |
| 329 | * @param [in] char* in_obj_name :Sensitive data name |
| 330 | * @retval GSW_HAL_SUCCESS is success\other is fail |
| 331 | */ |
| 332 | int32_t gsw_tee_delete_secure_data(const char* in_obj_name) |
| 333 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 334 | LOGE(GSW_TEE,"delete start\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 335 | if (in_obj_name == NULL) |
| 336 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 337 | int32_t ret = 0; |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 338 | |
| 339 | TEEC_Result res = delete_secure_object(&ctx, in_obj_name); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 340 | if (res != TEEC_SUCCESS) |
| 341 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 342 | LOGE(GSW_TEE,"Failed to delete the object: 0x%x", res); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 343 | ret = GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 344 | } |
lichengzhang | 36264d6 | 2025-07-30 15:55:35 +0800 | [diff] [blame] | 345 | bool is_pubkey = !strncmp(in_obj_name, PUBLIC_KEY_SEC_NAME, strlen(PUBLIC_KEY_SEC_NAME)); |
| 346 | bool is_level1 = !strncmp(in_obj_name, SEC_LEVEL1_SEC_NAME, strlen(SEC_LEVEL1_SEC_NAME)); |
| 347 | if (is_pubkey || is_level1) |
| 348 | { |
| 349 | tmp_sec *target = is_pubkey ? &public_key_sec : &sec_level1_sec; |
| 350 | target->length = 0; |
| 351 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 352 | LOGE(GSW_TEE,"delete really end\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 353 | return ret; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
| 357 | * @brief check sensitive data from tee |
| 358 | * @param [in] char* in_obj_name :Sensitive data name |
| 359 | * @retval GSW_HAL_SUCCESS is exist\ other is not exist or fail |
| 360 | */ |
| 361 | int32_t gsw_tee_check_secure_data(const char* in_obj_name) |
| 362 | { |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 363 | if (in_obj_name == NULL) |
| 364 | return GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 365 | int32_t ret = 1; |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 366 | size_t size = basic_buf_len; |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 367 | char *tmp_buf = (char*)malloc(basic_buf_len); |
| 368 | if (NULL == tmp_buf) |
| 369 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 370 | LOGE(GSW_TEE,"Failed malloc fail"); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 371 | return GSW_HAL_NO_MEMORY; |
| 372 | } |
lichengzhang | 0e5969b | 2025-07-09 11:02:55 +0800 | [diff] [blame] | 373 | TEEC_Result res = read_secure_object(&ctx, in_obj_name, tmp_buf, &size); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 374 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) |
| 375 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 376 | LOGE(GSW_TEE,"the obj no found\n"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 377 | ret = GSW_HAL_ERROR_TEE_SFS_FILE_NOEXIST; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 378 | } |
| 379 | else if (res == TEEC_SUCCESS) |
| 380 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 381 | LOGE(GSW_TEE,"the obj is exist\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 382 | ret = GSW_HAL_SUCCESS; |
| 383 | } |
| 384 | else |
| 385 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 386 | LOGE(GSW_TEE,"Failed to read an object from the secure storage"); |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 387 | ret = GSW_HAL_NORMAL_FAIL; |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 388 | } |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 389 | free(tmp_buf); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 390 | return ret; |
| 391 | } |
| 392 | |
lichengzhang | d7aea6c | 2025-06-05 16:35:54 +0800 | [diff] [blame] | 393 | /** |
| 394 | * @brief deinit tee sdk |
| 395 | * @param [in] None |
| 396 | * @param [out] None |
| 397 | * @retval GSW_HAL_SUCCESS is success\other is fail |
| 398 | */ |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 399 | int32_t gsw_tee_sdk_deinit(void) |
| 400 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 401 | LOGE(GSW_TEE,"deinit start\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 402 | if (terminate_tee_session) { |
| 403 | terminate_tee_session(&ctx); // 终止TEE会话 |
| 404 | terminate_tee_session = NULL; |
| 405 | } |
| 406 | |
| 407 | if (dlHandle_secure) { |
| 408 | dlclose(dlHandle_secure); // 卸载安全库 |
| 409 | dlHandle_secure = NULL; |
| 410 | } |
| 411 | |
| 412 | if (dlHandle_mbtk) { |
| 413 | dlclose(dlHandle_mbtk); // 卸载日志库 |
| 414 | dlHandle_mbtk = NULL; |
| 415 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 416 | LOGE(GSW_TEE,"deinit end\n"); |
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 417 | return GSW_HAL_SUCCESS; |
| 418 | } |
| 419 | |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 420 | int32_t gsw_secure_init(void) |
| 421 | { |
| 422 | static int s_init_flag = 0; |
| 423 | if (0xAA55 == s_init_flag) |
| 424 | { |
| 425 | return GSW_HAL_SUCCESS; |
| 426 | } |
| 427 | if (GSW_HAL_SUCCESS == gsw_tee_sdk_init()) |
| 428 | { |
| 429 | s_init_flag = 0xAA55; |
| 430 | return GSW_HAL_SUCCESS; |
| 431 | } |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 432 | LOGE(GSW_TEE,"secure sdk init fail!!!"); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 433 | return GSW_HAL_NORMAL_FAIL; |
| 434 | } |
| 435 | |
| 436 | int32_t gsw_secure_storage_query(const char *objname, int32_t *exist_state) |
| 437 | { |
| 438 | #if 0 |
| 439 | if (NULL == objname || NULL == exist_state) |
| 440 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 441 | LOGE(GSW_TEE,"storage query input param error objname %p, exist_state %p",objname, exist_state); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 442 | return GSW_HAL_ARG_INVALID; |
| 443 | } |
| 444 | int32_t ret = gsw_secure_init(); |
| 445 | if (GSW_HAL_SUCCESS != ret) |
| 446 | { |
| 447 | return ret; |
| 448 | } |
| 449 | ret = gsw_tee_check_secure_data(objname); |
| 450 | if (GSW_HAL_SUCCESS == ret) |
| 451 | { |
| 452 | *exist_state = 1; //表明数据存在 |
| 453 | } |
| 454 | else |
| 455 | { |
| 456 | *exist_state = 0; |
| 457 | } |
| 458 | return ret; |
| 459 | #else |
| 460 | return GSW_HAL_NO_SUPPROT; |
| 461 | #endif |
| 462 | } |
| 463 | |
| 464 | int32_t gsw_secure_storage_read(const char *objname, uint8_t *outbuf, uint32_t buflen, uint32_t *outlen) |
| 465 | { |
| 466 | #if 0 |
| 467 | if (NULL == objname || NULL == outbuf || NULL == outlen) |
| 468 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 469 | LOGE(GSW_TEE,"storage read input param error objname %p, outbuf %p, outlen %p",objname, outbuf, outlen); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 470 | return GSW_HAL_ARG_INVALID; |
| 471 | } |
| 472 | int32_t ret = gsw_secure_init(); |
| 473 | if (GSW_HAL_SUCCESS != ret) |
| 474 | { |
| 475 | return ret; |
| 476 | } |
| 477 | *outlen = buflen; |
| 478 | return gsw_tee_read_secure_data(objname, (char*)outbuf, outlen); |
| 479 | #else |
| 480 | return GSW_HAL_NO_SUPPROT; |
| 481 | #endif |
| 482 | |
| 483 | } |
| 484 | |
| 485 | int32_t gsw_secure_storage_write(const char *objname, const uint8_t *inbuf, uint32_t inlen) |
| 486 | { |
| 487 | #if 0 |
| 488 | if (NULL == objname || NULL == inbuf || 0 == inlen) |
| 489 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 490 | LOGE(GSW_TEE,"storage write input param error objname %p, outbuf %p, inlen %u",objname, inbuf, inlen); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 491 | return GSW_HAL_ARG_INVALID; |
| 492 | } |
| 493 | int32_t ret = gsw_secure_init(); |
| 494 | if (GSW_HAL_SUCCESS != ret) |
| 495 | { |
| 496 | return ret; |
| 497 | } |
| 498 | return gsw_tee_write_secure_data(objname, (char*)inbuf, inlen); |
| 499 | #else |
| 500 | return GSW_HAL_NO_SUPPROT; |
| 501 | #endif |
| 502 | } |
| 503 | |
| 504 | int32_t gsw_secure_storage_delete(const char *objname) |
| 505 | { |
| 506 | #if 0 |
| 507 | if (NULL == objname) |
| 508 | { |
lichengzhang | 7715b2f | 2025-07-19 10:18:21 +0800 | [diff] [blame] | 509 | LOGE(GSW_TEE,"storage delete input param error objname %p",objname); |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 510 | return GSW_HAL_ARG_INVALID; |
| 511 | } |
| 512 | int32_t ret = gsw_secure_init(); |
| 513 | if (GSW_HAL_SUCCESS != ret) |
| 514 | { |
| 515 | return ret; |
| 516 | } |
| 517 | return gsw_tee_delete_secure_data(objname); |
| 518 | #else |
| 519 | return GSW_HAL_NO_SUPPROT; |
| 520 | #endif |
| 521 | } |