文档管理中心
示例代码设备安全能力TA快速开发TA示例代码

TA示例代码

TA为运行在TEE环境中的可执行文件,TA可执行文件的入口函数以及接收CA侧调用请求的函数为Global Platform标准中定义的接口,以下代码展示了TA接收CA请求并返回处理结果给CA的简单示例。

收起
自动换行
深色代码主题
复制
  1. #include <tee_internal_api.h>
  2. #include <tee_ext_api.h>
  3. #include <tee_log.h>
  4. #include <securec.h>
  5. #define TA_TEMPLATE_VERSION "demo_20200601"
  6. #define OUT_BUFFER_INDEX 3
  7. enum {
  8. CMD_GET_TA_VERSION = 1,
  9. };
  10. static TEE_Result get_ta_version(char* buffer, size_t *buf_len)
  11. {
  12. char *version = TA_TEMPLATE_VERSION;
  13. if (*buf_len < strlen(version) + 1) {
  14. tloge("buffer is too short for storing result");
  15. *buf_len = strlen(version) + 1;
  16. return TEE_ERROR_SHORT_BUFFER;
  17. }
  18. errno_t err = strncpy_s(buffer, *buf_len, version, strlen(version) + 1);
  19. if (err != EOK)
  20. return TEE_ERROR_SECURITY;
  21. *buf_len = strlen(version) + 1;
  22. return TEE_SUCCESS;
  23. }
  24. /**
  25. * Function TA_CreateEntryPoint
  26. * Description:
  27. * The function TA_CreateEntryPoint is the Trusted Application's constructor,
  28. * which the Framework calls when it creates a new instance of this Trusted Application.
  29. */
  30. TEE_Result TA_CreateEntryPoint(void)
  31. {
  32. tlogd("----- TA entry point ----- ");
  33. tlogd("TA version: %s", TA_TEMPLATE_VERSION);
  34. char *package_name = "com.huawei.itrustee.helloworld";
  35. char *modulus =
  36. "c5169effcc46070be2e7389eefe57d3a4bb66e8e7504ab3ae6815cfbf013b7fc83c92623a84a5b8c03f"
  37. "9203091894cc997b7364eae2af38ffea83f13b8d8da3d56d756e74f176810ac742a6cd4bbd257c85e31"
  38. "6dfc8fd5bedad60b6358a8b55e2d55d90f742b70e856a170a2ae0cd51aae50f755de0560a5522f06167"
  39. "c4b2d028e9a396241c2c5aec242a1f513a6cc6f95f3f383417de94f9336761c7f5df3452467e314abf5"
  40. "3381730b8b91cb5e3890801e588a5e6c0492f4d42f014ddca4604260fd0dfce056364e2cb0fcfaee89f"
  41. "77ceab57cb0453e6fd6680af47062d572f367a62d63907ed8ff5bf3116a6c7b56c1f4107c81195b41bd"
  42. "95b5bd266d1935";
  43. char *public_exponent = "10001";
  44. if (TEE_SUCCESS == AddCaller_CA_apk(package_name, modulus, public_exponent)) {
  45. tlogd("TA entry point: add ca whitelist success");
  46. } else {
  47. tloge("TA_entry point: add ca whitelist failed");
  48. return TEE_ERROR_GENERIC;
  49. }
  50. return TEE_SUCCESS;
  51. }
  52. /**
  53. * Function TA_OpenSessionEntryPoint
  54. * Description:
  55. * The Framework calls the function TA_OpenSessionEntryPoint
  56. * when a client requests to open a session with the Trusted Application.
  57. * The open session request may result in a new Trusted Application instance
  58. * being created.
  59. */
  60. TEE_Result TA_OpenSessionEntryPoint(uint32_t parm_type,
  61. TEE_Param params[4], void** session_context)
  62. {
  63. (void)parm_type;
  64. (void)params;
  65. (void)session_context;
  66. tlogd("---- TA open session -------- ");
  67. return TEE_SUCCESS;
  68. }
  69. /**
  70. * Function TA_InvokeCommandEntryPoint:
  71. * Description:
  72. * The Framework calls this function when the client invokes a command
  73. * within the given session.
  74. */
  75. TEE_Result TA_InvokeCommandEntryPoint(void* session_context, uint32_t cmd,
  76. uint32_t parm_type, TEE_Param params[4])
  77. {
  78. TEE_Result ret;
  79. (void)session_context;
  80. tlogd("---- TA invoke command ----------- ");
  81. switch (cmd) {
  82. case CMD_GET_TA_VERSION:
  83. if (!check_param_type(parm_type,
  84. TEE_PARAM_TYPE_NONE,
  85. TEE_PARAM_TYPE_NONE,
  86. TEE_PARAM_TYPE_NONE,
  87. TEE_PARAM_TYPE_MEMREF_OUTPUT)) {
  88. tloge("Bad expected parameter types");
  89. return TEE_ERROR_BAD_PARAMETERS;
  90. }
  91. if (params[OUT_BUFFER_INDEX].memref.buffer == NULL ||
  92. params[OUT_BUFFER_INDEX].memref.size == 0) {
  93. tloge("InvokeCommand with bad, cmd is %u", cmd);
  94. return TEE_ERROR_BAD_PARAMETERS;
  95. }
  96. ret = get_ta_version(params[OUT_BUFFER_INDEX].memref.buffer, &params[OUT_BUFFER_INDEX].memref.size);
  97. if (ret != TEE_SUCCESS) {
  98. tloge("InvokeCommand Failed 0x%x. cmd is %u", ret, cmd);
  99. return ret;
  100. }
  101. break;
  102. default:
  103. tloge("Unknown cmd is %u", cmd);
  104. ret = TEE_ERROR_BAD_PARAMETERS;
  105. }
  106. return ret;
  107. }
  108. /**
  109. * Function TA_CloseSessionEntryPoint:
  110. * Description:
  111. * The Framework calls this function to close a client session.
  112. * During the call to this function the implementation can use
  113. * any session functions.
  114. */
  115. void TA_CloseSessionEntryPoint(void* session_context)
  116. {
  117. (void)session_context;
  118. tlogd("---- close session ----- ");
  119. }
  120. /**
  121. * Function TA_DestroyEntryPoint
  122. * Description:
  123. * The function TA_DestroyEntryPoint is the Trusted Application's destructor,
  124. * which the Framework calls when the instance is being destroyed.
  125. */
  126. void TA_DestroyEntryPoint(void)
  127. {
  128. tlogd("---- destory TA ---- ");
  129. }
在 示例代码 中进行搜索
请输入您想要搜索的关键词