TA为运行在TEE环境中的可执行文件,TA可执行文件的入口函数以及接收CA侧调用请求的函数为Global Platform标准中定义的接口,以下代码展示了TA接收CA请求并返回处理结果给CA的简单示例。
#include <tee_internal_api.h>
#include <tee_ext_api.h>
#include <tee_log.h>
#include <securec.h>
#define TA_TEMPLATE_VERSION "demo_20200601"
#define OUT_BUFFER_INDEX 3
enum {
CMD_GET_TA_VERSION = 1,
};
static TEE_Result get_ta_version(char* buffer, size_t *buf_len)
{
char *version = TA_TEMPLATE_VERSION;
if (*buf_len < strlen(version) + 1) {
tloge("buffer is too short for storing result");
*buf_len = strlen(version) + 1;
return TEE_ERROR_SHORT_BUFFER;
}
errno_t err = strncpy_s(buffer, *buf_len, version, strlen(version) + 1);
if (err != EOK)
return TEE_ERROR_SECURITY;
*buf_len = strlen(version) + 1;
return TEE_SUCCESS;
}
/**
* Function TA_CreateEntryPoint
* Description:
* The function TA_CreateEntryPoint is the Trusted Application's constructor,
* which the Framework calls when it creates a new instance of this Trusted Application.
*/
TEE_Result TA_CreateEntryPoint(void)
{
tlogd("----- TA entry point ----- ");
tlogd("TA version: %s", TA_TEMPLATE_VERSION);
char *package_name = "com.huawei.itrustee.helloworld";
char *modulus =
"c5169effcc46070be2e7389eefe57d3a4bb66e8e7504ab3ae6815cfbf013b7fc83c92623a84a5b8c03f"
"9203091894cc997b7364eae2af38ffea83f13b8d8da3d56d756e74f176810ac742a6cd4bbd257c85e31"
"6dfc8fd5bedad60b6358a8b55e2d55d90f742b70e856a170a2ae0cd51aae50f755de0560a5522f06167"
"c4b2d028e9a396241c2c5aec242a1f513a6cc6f95f3f383417de94f9336761c7f5df3452467e314abf5"
"3381730b8b91cb5e3890801e588a5e6c0492f4d42f014ddca4604260fd0dfce056364e2cb0fcfaee89f"
"77ceab57cb0453e6fd6680af47062d572f367a62d63907ed8ff5bf3116a6c7b56c1f4107c81195b41bd"
"95b5bd266d1935";
char *public_exponent = "10001";
if (TEE_SUCCESS == AddCaller_CA_apk(package_name, modulus, public_exponent)) {
tlogd("TA entry point: add ca whitelist success");
} else {
tloge("TA_entry point: add ca whitelist failed");
return TEE_ERROR_GENERIC;
}
return TEE_SUCCESS;
}
/**
* Function TA_OpenSessionEntryPoint
* Description:
* The Framework calls the function TA_OpenSessionEntryPoint
* when a client requests to open a session with the Trusted Application.
* The open session request may result in a new Trusted Application instance
* being created.
*/
TEE_Result TA_OpenSessionEntryPoint(uint32_t parm_type,
TEE_Param params[4], void** session_context)
{
(void)parm_type;
(void)params;
(void)session_context;
tlogd("---- TA open session -------- ");
return TEE_SUCCESS;
}
/**
* Function TA_InvokeCommandEntryPoint:
* Description:
* The Framework calls this function when the client invokes a command
* within the given session.
*/
TEE_Result TA_InvokeCommandEntryPoint(void* session_context, uint32_t cmd,
uint32_t parm_type, TEE_Param params[4])
{
TEE_Result ret;
(void)session_context;
tlogd("---- TA invoke command ----------- ");
switch (cmd) {
case CMD_GET_TA_VERSION:
if (!check_param_type(parm_type,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_MEMREF_OUTPUT)) {
tloge("Bad expected parameter types");
return TEE_ERROR_BAD_PARAMETERS;
}
if (params[OUT_BUFFER_INDEX].memref.buffer == NULL ||
params[OUT_BUFFER_INDEX].memref.size == 0) {
tloge("InvokeCommand with bad, cmd is %u", cmd);
return TEE_ERROR_BAD_PARAMETERS;
}
ret = get_ta_version(params[OUT_BUFFER_INDEX].memref.buffer, ¶ms[OUT_BUFFER_INDEX].memref.size);
if (ret != TEE_SUCCESS) {
tloge("InvokeCommand Failed 0x%x. cmd is %u", ret, cmd);
return ret;
}
break;
default:
tloge("Unknown cmd is %u", cmd);
ret = TEE_ERROR_BAD_PARAMETERS;
}
return ret;
}
/**
* Function TA_CloseSessionEntryPoint:
* Description:
* The Framework calls this function to close a client session.
* During the call to this function the implementation can use
* any session functions.
*/
void TA_CloseSessionEntryPoint(void* session_context)
{
(void)session_context;
tlogd("---- close session ----- ");
}
/**
* Function TA_DestroyEntryPoint
* Description:
* The function TA_DestroyEntryPoint is the Trusted Application's destructor,
* which the Framework calls when the instance is being destroyed.
*/
void TA_DestroyEntryPoint(void)
{
tlogd("---- destory TA ---- ");
}