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

CA示例代码

CA为运行在REE环境中的应用程序,可以是动态库的形式集成到APK程序中或单独的执行文件。以下以独立可执行文件形式CA为例,展示CA与TA通信的简单流程。

收起
自动换行
深色代码主题
复制
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <sys/ioctl.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include "tee_client_api.h"
  11. #define VERSION_BUFFER_SIZE 256
  12. #define OPERATION_START_FLAG 1
  13. static const TEEC_UUID DEMO_TEMPLATE_UUID =
  14. {
  15. 0x58dbb3b9, 0x4a0c, 0x42d2,
  16. { 0xa8, 0x4d, 0x7c, 0x7a, 0xb1, 0x75, 0x39, 0xfc }
  17. };
  18. enum {
  19. CMD_GET_TA_VERSION = 1,
  20. };
  21. int main(void)
  22. {
  23. TEEC_Context context = {0};
  24. TEEC_Session session = {0};
  25. TEEC_Result result = {0};
  26. TEEC_Operation operation = {0};
  27. uint32_t origin = {0};
  28. char versionBuf[VERSION_BUFFER_SIZE] = {0};
  29. unsigned int bufLen = VERSION_BUFFER_SIZE;
  30. result = TEEC_InitializeContext(NULL, &context);
  31. if (result != TEEC_SUCCESS) {
  32. TEEC_Error("teec initial failed");
  33. goto cleanup_1;
  34. }
  35. /* MUST use TEEC_LOGIN_IDENTIFY method */
  36. operation.started = OPERATION_START_FLAG;
  37. operation.paramTypes = TEEC_PARAM_TYPES(
  38. TEEC_NONE,
  39. TEEC_NONE,
  40. TEEC_MEMREF_TEMP_INPUT,
  41. TEEC_MEMREF_TEMP_INPUT);
  42. result = TEEC_OpenSession(
  43. &context, &session, &DEMO_TEMPLATE_UUID, TEEC_LOGIN_IDENTIFY, NULL, &operation, &origin);
  44. if (result != TEEC_SUCCESS) {
  45. TEEC_Error("teec open session failed");
  46. goto cleanup_2;
  47. } else {
  48. TEEC_Debug("teec open session successed");
  49. }
  50. operation.started = OPERATION_START_FLAG;
  51. operation.paramTypes = TEEC_PARAM_TYPES(
  52. TEEC_NONE,
  53. TEEC_NONE,
  54. TEEC_NONE,
  55. TEEC_MEMREF_TEMP_OUTPUT);
  56. operation.params[3].tmpref.buffer = versionBuf;
  57. operation.params[3].tmpref.size = bufLen;
  58. result = TEEC_InvokeCommand(&session, CMD_GET_TA_VERSION, &operation, &origin);
  59. if (result != TEEC_SUCCESS) {
  60. TEEC_Error("invoke failed, codes=0x%x, origin=0x%x", result, origin);
  61. } else {
  62. printf("Succeed to load TA, TA's version: %s.\n", versionBuf);
  63. }
  64. TEEC_CloseSession(&session);
  65. cleanup_2:
  66. TEEC_FinalizeContext(&context);
  67. cleanup_1:
  68. return 0;
  69. }
在 示例代码 中进行搜索
请输入您想要搜索的关键词