blob: 2ba8ece1769a8d45f2273d2535c432a8e3252db7 [file] [log] [blame]
#include "gsw/gsw_tee.h"
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <tee_client_api.h>
#ifndef LOG_ERR_LEVEL
#define LOG_ERR_LEVEL 3 /* error conditions */
#endif
#ifndef LOG_WARN_LEVEL
#define LOG_WARN_LEVEL 4 /* warning conditions */
#endif
#ifndef LOG_INFO_LEVEL
#define LOG_INFO_LEVEL 6 /* informational */
#endif
#ifndef LOG_DEBUG_LEVEL
#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
#endif
#ifndef LOG_VERBOSE_LEVEL
#define LOG_VERBOSE_LEVEL 8
#endif
#define LOGV(fmt, args ...) \
do{ \
char *file_ptr_1001 = __FILE__; \
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
char line_1001[10] = {0}; \
sprintf(line_1001, "%d", __LINE__); \
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
if(*ptr_1001 == '/') \
break; \
ptr_1001--; \
} \
mbtk_log(LOG_VERBOSE_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
} while(0)
#define LOGI(fmt, args...) \
do{ \
char *file_ptr_1001 = __FILE__; \
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
char line_1001[10] = {0}; \
sprintf(line_1001, "%d", __LINE__); \
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
if(*ptr_1001 == '/') \
break; \
ptr_1001--; \
} \
mbtk_log(LOG_INFO_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
} while(0)
#define LOGD(fmt, args...) \
do{ \
char *file_ptr_1001 = __FILE__; \
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
char line_1001[10] = {0}; \
sprintf(line_1001, "%d", __LINE__); \
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
if(*ptr_1001 == '/') \
break; \
ptr_1001--; \
} \
mbtk_log(LOG_DEBUG_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
} while(0)
#define LOGW(fmt, args...) \
do{ \
char *file_ptr_1001 = __FILE__; \
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
char line_1001[10] = {0}; \
sprintf(line_1001, "%d", __LINE__); \
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
if(*ptr_1001 == '/') \
break; \
ptr_1001--; \
} \
mbtk_log(LOG_WARN_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
} while(0)
#define LOGE(fmt, args...) \
do{ \
char *file_ptr_1001 = __FILE__; \
char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
char line_1001[10] = {0}; \
sprintf(line_1001, "%d", __LINE__); \
while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
if(*ptr_1001 == '/') \
break; \
ptr_1001--; \
} \
mbtk_log(LOG_ERR_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
} while(0)
struct test_ctx {
TEEC_Context ctx;
TEEC_Session sess;
};
struct test_ctx ctx;
#define lib_secure_path "/lib/libsecure_storage.so"
static void *dlHandle_secure;
#define lib_mbtk_path "/lib/libmbtk_lib.so"
static void *dlHandle_mbtk;
static void (*mbtk_log)(int level, const char *format, ...);
static void (*mbtk_log_init)(char *path, char *tag);
int (*prepare_tee_session)(struct test_ctx *ctx);
void (*terminate_tee_session)(struct test_ctx *ctx);
TEEC_Result (*read_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t data_len);
TEEC_Result (*write_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t data_len);
TEEC_Result (*delete_secure_object)(struct test_ctx *ctx, const char *id);
static int tee_api_import(void)
{
dlHandle_mbtk = dlopen(lib_mbtk_path, RTLD_NOW);
if (dlHandle_mbtk == NULL)
{
return GSW_HAL_FAIL;
}
dlHandle_secure = dlopen(lib_secure_path, RTLD_NOW);
if (dlHandle_secure == NULL)
{
return GSW_HAL_FAIL;
}
mbtk_log_init = (void (*)(char *path, char *tag))dlsym(dlHandle_mbtk, "mbtk_log_init");
if (mbtk_log_init == NULL)
{
return GSW_HAL_FAIL;
}
mbtk_log = (void (*)(int level, const char *format, ...))dlsym(dlHandle_mbtk, "mbtk_log");
if (mbtk_log == NULL)
{
return GSW_HAL_FAIL;
}
prepare_tee_session = (int (*)(struct test_ctx *ctx))dlsym(dlHandle_secure, "prepare_tee_session");
if (prepare_tee_session == NULL)
{
LOGE("prepare_tee_session dlsym fail\n");
return GSW_HAL_FAIL;
}
terminate_tee_session = (void (*)(struct test_ctx *ctx))dlsym(dlHandle_secure, "terminate_tee_session");
if (terminate_tee_session == NULL)
{
LOGE("terminate_tee_session dlsym fail\n");
return GSW_HAL_FAIL;
}
read_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id,char *data, size_t data_len))dlsym(dlHandle_secure, "read_secure_object");
if (read_secure_object == NULL)
{
LOGE("read_secure_object dlsym fail\n");
return GSW_HAL_FAIL;
}
write_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id,char *data, size_t data_len))dlsym(dlHandle_secure, "write_secure_object");
if (write_secure_object == NULL)
{
LOGE("write_secure_object dlsym fail\n");
return GSW_HAL_FAIL;
}
delete_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id))dlsym(dlHandle_secure, "delete_secure_object");
if (delete_secure_object == NULL)
{
LOGE("delete_secure_object dlsym fail\n");
return GSW_HAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
/**
* @brief init tee sdk
* @param [in] None
* @param [out] None
* @retval GSW_HAL_SUCCESS is success\other is fail
*/
int32_t gsw_tee_sdk_init(void)
{
int32_t ret = 0;
ret = tee_api_import();
if(ret)
{
LOGE("tee_api_import fail\n");
return ret;
}
ret = prepare_tee_session(&ctx);
return ret;
}
/**
* @brief read sensitive data from tee
* @param [in] char* in_obj_name :Sensitive data name
* @param [in] unsigned int* p_out_buf_len:The size of sensitive data output cache
* @param [out] char* out_buf:Cache of sensitive data output
* @param [out] unsigned int* p_out_buf_len:Sensitive data length
* @retval GSW_HAL_SUCCESS is success\other is fail
*/
#define basic_buf_len 7000
int32_t gsw_tee_read_secure_data(const char* in_obj_name, char* out_buf, unsigned int* p_out_buf_len)
{
int32_t ret = 0;
TEEC_Result res;
res = read_secure_object(&ctx, in_obj_name, out_buf, basic_buf_len);
if (res != TEEC_SUCCESS)
{
LOGE("Failed to read an object from the secure storage");
ret = -1;
}
*p_out_buf_len = strlen(out_buf);
return ret;
}
/**
* @brief write sensitive data to tee
* @param [in] char* in_obj_name :Sensitive data name
* @param [in] char* in_buf:A cache for writing sensitive data
* @param [out] unsigned int in_buf_len:Sensitive data length
* @retval GSW_HAL_SUCCESS is success\other is fail
*/
int32_t gsw_tee_write_secure_data(const char* in_obj_name, char* in_buf, unsigned int in_buf_len)
{
int32_t ret = 0;
TEEC_Result res;
res = write_secure_object(&ctx, in_obj_name,in_buf, in_buf_len);
if (res != TEEC_SUCCESS)
{
LOGE("Failed to write an object from the secure storage");
ret = -1;
}
return ret;
}
/**
* @brief delete sensitive data from tee
* @param [in] char* in_obj_name :Sensitive data name
* @retval GSW_HAL_SUCCESS is success\other is fail
*/
int32_t gsw_tee_delete_secure_data(const char* in_obj_name)
{
int32_t ret = 0;
TEEC_Result res;
res = delete_secure_object(&ctx, in_obj_name);
if (res != TEEC_SUCCESS)
{
LOGE("Failed to delete the object: 0x%x", res);
ret = -1;
}
return ret;
}
/**
* @brief check sensitive data from tee
* @param [in] char* in_obj_name :Sensitive data name
* @retval GSW_HAL_SUCCESS is exist\ other is not exist or fail
*/
int32_t gsw_tee_check_secure_data(const char* in_obj_name)
{
int32_t ret = 1;
TEEC_Result res;
char out_buf[4];
res = read_secure_object(&ctx, in_obj_name, out_buf, sizeof(out_buf));
if (res == TEEC_ERROR_ITEM_NOT_FOUND)
{
LOGE("the obj no found\n");
ret = GSW_HAL_NOFOUND;
}
else if (res == TEEC_SUCCESS)
{
LOGE("the obj is exist\n");
ret = GSW_HAL_SUCCESS;
}
else
{
LOGE("Failed to read an object from the secure storage");
ret = GSW_HAL_FAIL;
}
return ret;
}
int32_t gsw_tee_sdk_deinit(void)
{
if (terminate_tee_session) {
terminate_tee_session(&ctx); // 终止TEE会话
terminate_tee_session = NULL;
}
if (dlHandle_secure) {
dlclose(dlHandle_secure); // 卸载安全库
dlHandle_secure = NULL;
}
if (dlHandle_mbtk) {
dlclose(dlHandle_mbtk); // 卸载日志库
dlHandle_mbtk = NULL;
}
return GSW_HAL_SUCCESS;
}