参考内容《手机安全和可信应用开发》
https://note.youdao.com/s/MTlG4c1w
介绍
TA的全称是Trust Application, 即可信任应用程序。 CA的全称是Client Applicant, 即客户端应用程序。 TA运行在OP-TEE的用户空间, CA运行在REE侧。 CA执行时代入特定的UUID和命令ID参数就能实现请求特定TA执行特定操作的需求, 并将执行结果返回给CA。 通过CA对TA的调用可实现在REE侧对安全设备和安全资源的操作。 普通用户无法知道TA的具体实现, 例如操作使用了什么算法、 操作了哪些资源、 获取了哪些数据等, 这也就确保了相关资源和数据的安全。
编写自己的第一个TA程序
Android.mk是安卓编译,
host/main.c==>CA:共五步:1.初始化上下文;2.打开会话;3.发送命令和参数;4.关闭会话;5.结束上下文。
hello_world_ta.c==>TA,分别对应CA五步的入口操作
CA
int main(void)
{
//1.-------------------------------------------------------------------------------------------- res = TEEC_InitializeContext(NULL, &ctx);if (res != TEEC_SUCCESS)errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
//2.--------------------------------------------------------------------------------------------res = TEEC_OpenSession(&ctx, &sess, &uuid,TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);if (res != TEEC_SUCCESS)errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",res, err_origin);
//3.-------------------------------------------------------------------------------------------- memset(&op, 0, sizeof(op));op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE,TEEC_NONE, TEEC_NONE);op.params[0].value.a = 42;printf("Invoking TA to increment %d\n", op.params[0].value.a);res = TEEC_InvokeCommand(&sess, TA_HELLO_WORLD_CMD_INC_VALUE, &op,&err_origin);command if (res != TEEC_SUCCESS)errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",res, err_origin);printf("TA incremented value to %d\n", op.params[0].value.a);
//4.---------------------------------------------------------------------------------------------TEEC_CloseSession(&sess);
//5.--------------------------------------------------------------------------------------------TEEC_FinalizeContext(&ctx);return 0;
}
TA
TEE_Result TA_CreateEntryPoint(void)
{DMSG("has been called");return TEE_SUCCESS;
}void TA_DestroyEntryPoint(void)
{DMSG("has been called");
}TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types,TEE_Param __maybe_unused params[4],void __maybe_unused **sess_ctx)
{uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE);DMSG("has been called");if (param_types != exp_param_types)return TEE_ERROR_BAD_PARAMETERS;(void)¶ms;(void)&sess_ctx;IMSG("Hello 0xCC!\n");return TEE_SUCCESS;
}void TA_CloseSessionEntryPoint(void __maybe_unused *sess_ctx)
{(void)&sess_ctx; /* Unused parameter */IMSG("Goodbye! 0xCC \n");
}static TEE_Result inc_value(uint32_t param_types,TEE_Param params[4])
{uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE);DMSG("has been called");if (param_types != exp_param_types)return TEE_ERROR_BAD_PARAMETERS;IMSG("Got value: %u from NW", params[0].value.a);params[0].value.a += 10; ///42+10 = 52IMSG("Increase value to: %u", params[0].value.a);return TEE_SUCCESS;
}static TEE_Result dec_value(uint32_t param_types,TEE_Param params[4])
{uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE);DMSG("has been called");if (param_types != exp_param_types)return TEE_ERROR_BAD_PARAMETERS;IMSG("Got value: %u from NW", params[0].value.a);params[0].value.a--;IMSG("Decrease value to: %u", params[0].value.a);return TEE_SUCCESS;
}TEE_Result TA_InvokeCommandEntryPoint(void __maybe_unused *sess_ctx,uint32_t cmd_id,uint32_t param_types, TEE_Param params[4])
{(void)&sess_ctx; /* Unused parameter */switch (cmd_id) {case TA_HELLO_0xCC_CMD_INC_VALUE:return inc_value(param_types, params);case TA_HELLO_0xCC_CMD_DEC_VALUE:return dec_value(param_types, params);default:return TEE_ERROR_BAD_PARAMETERS;}
}
TA属性说明
#ifndef USER_TA_HEADER_DEFINES_H
#define USER_TA_HEADER_DEFINES_H/* To get the TA UUID definition */
#include <hello_0xcc_ta.h>#define TA_UUID TA_HELLO_0xCC_UUID
TA识别号
/** TA properties: multi-instance TA, no specific attribute* TA_FLAG_EXEC_DDR is meaningless but mandated.*/
#define TA_FLAGS TA_FLAG_EXEC_DDR
表示TA是否支持多个会话实例
/* Provisioned stack size */
#define TA_STACK_SIZE (2 * 1024)
TA运行时栈大小
/* Provisioned heap size for TEE_Malloc() and friends */
#define TA_DATA_SIZE (32 * 1024)
TA运行时堆空间大小 TEE_Malloc就是从这个空间中分配
/* The gpd.ta.version property */
#define TA_VERSION "1.0"
版本信息
/* The gpd.ta.description property */
#define TA_DESCRIPTION "Example of OP-TEE Hello 0xCC Trusted Application"
TA的描述字段
/* Extra properties */
#define TA_CURRENT_TA_EXT_PROPERTIES \{ "org.linaro.optee.examples.hello_world.property1", \USER_TA_PROP_TYPE_STRING, \"Some string" }, \{ "org.linaro.optee.examples.hello_world.property2", \USER_TA_PROP_TYPE_U32, &(const uint32_t){ 0x0010 } }
通过宏定义描述gp.ta.description和gp.ta.version
#endif /* USER_TA_HEADER_DEFINES_H */
运行结果
逆向
将uuid.ta的程序逆向,可以看到是跟编写的代码一致的,一般是从TA_InvokeCommandEntryPoint函数开始看起,因为大部分功能是从这里来的。