• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 
13 #include <tee_ext_api.h>
14 #include <tee_log.h>
15 #include <securec.h>
16 
17 #define TA_TEMPLATE_VERSION "demo_20200601"
18 #define PARAM_COUNT      4
19 #define OUT_BUFFER_INDEX 3
20 
21 enum {
22     CMD_GET_TA_VERSION = 1,
23 };
24 
get_ta_version(char * buffer,size_t * buf_len)25 static TEE_Result get_ta_version(char* buffer, size_t *buf_len)
26 {
27     const char *version = TA_TEMPLATE_VERSION;
28 
29     if (*buf_len < strlen(version) + 1) {
30         tloge("buffer is too short for storing result");
31         *buf_len = strlen(version) + 1;
32         return TEE_ERROR_SHORT_BUFFER;
33     }
34 
35     errno_t err = strncpy_s(buffer, *buf_len, version, strlen(version) + 1);
36     if (err != EOK)
37         return TEE_ERROR_SECURITY;
38 
39     *buf_len = strlen(version) + 1;
40 
41     return TEE_SUCCESS;
42 }
43 
44 /**
45  * Function TA_CreateEntryPoint
46  * Description:
47  *   The function TA_CreateEntryPoint is the Trusted Application's constructor,
48  *   which the Framework calls when it creates a new instance of this Trusted Application.
49  */
TA_CreateEntryPoint(void)50 TEE_Result TA_CreateEntryPoint(void)
51 {
52     TEE_Result ret;
53 
54     tlogd("----- TA entry point ----- ");
55     tlogd("TA version: %s", TA_TEMPLATE_VERSION);
56 
57     // cmdline: /vendor/bin/teec_hello, uid: 0
58     const uint8_t hash[] = {
59         0xca, 0x9f, 0x5e, 0xd7, 0x6d, 0x7, 0xd, 0x66,
60         0xe7, 0xb2, 0xab, 0xb3, 0x55, 0xfc, 0xb0, 0xbf,
61         0xc8, 0x16, 0x52, 0x37, 0x5f, 0xfe, 0x99, 0xfc,
62         0x34, 0x43, 0xf6, 0x5f, 0xc, 0x70, 0x44, 0x48,
63     };
64     ret = AddCaller_CA(hash, sizeof(hash));
65     if (ret == TEE_SUCCESS) {
66         tlogd("TA entry point: add ca whitelist success");
67     } else {
68         tloge("TA entry point: add ca whitelist failed");
69         return TEE_ERROR_GENERIC;
70     }
71 
72     return TEE_SUCCESS;
73 }
74 
75 /**
76  * Function TA_OpenSessionEntryPoint
77  * Description:
78  *   The Framework calls the function TA_OpenSessionEntryPoint
79  *   when a client requests to open a session with the Trusted Application.
80  *   The open session request may result in a new Trusted Application instance
81  *   being created.
82  */
TA_OpenSessionEntryPoint(uint32_t parm_type,TEE_Param params[PARAM_COUNT],void ** session_context)83 TEE_Result TA_OpenSessionEntryPoint(uint32_t parm_type,
84     TEE_Param params[PARAM_COUNT], void** session_context)
85 {
86     (void)parm_type;
87     (void)params;
88     (void)session_context;
89     tlogd("---- TA open session -------- ");
90 
91     return TEE_SUCCESS;
92 }
93 
94 /**
95  * Function TA_InvokeCommandEntryPoint
96  * Description:
97  *   The Framework calls this function when the client invokes a command
98  *   within the given session.
99  */
TA_InvokeCommandEntryPoint(void * session_context,uint32_t cmd,uint32_t parm_type,TEE_Param params[PARAM_COUNT])100 TEE_Result TA_InvokeCommandEntryPoint(void* session_context, uint32_t cmd,
101     uint32_t parm_type, TEE_Param params[PARAM_COUNT])
102 {
103     TEE_Result ret;
104     (void)session_context;
105 
106     tlogd("---- TA invoke command ----------- ");
107     switch (cmd) {
108     case CMD_GET_TA_VERSION:
109         if (!check_param_type(parm_type,
110             TEE_PARAM_TYPE_NONE,
111             TEE_PARAM_TYPE_NONE,
112             TEE_PARAM_TYPE_NONE,
113             TEE_PARAM_TYPE_MEMREF_OUTPUT)) {
114             tloge("Bad expected parameter types");
115             return TEE_ERROR_BAD_PARAMETERS;
116         }
117         if (params[OUT_BUFFER_INDEX].memref.buffer == NULL ||
118             params[OUT_BUFFER_INDEX].memref.size == 0) {
119             tloge("InvokeCommand with bad, cmd is %u", cmd);
120             return TEE_ERROR_BAD_PARAMETERS;
121         }
122         ret = get_ta_version(params[OUT_BUFFER_INDEX].memref.buffer, &params[OUT_BUFFER_INDEX].memref.size);
123         if (ret != TEE_SUCCESS) {
124             tloge("InvokeCommand Failed 0x%x. cmd is %u", ret, cmd);
125             return ret;
126         }
127         break;
128     default:
129         tloge("Unknown cmd is %u", cmd);
130         ret = TEE_ERROR_BAD_PARAMETERS;
131     }
132 
133     return  ret;
134 }
135 
136 /**
137  * Function TA_CloseSessionEntryPoint
138  * Description:
139  *   The Framework calls this function to close a client session.
140  *   During the call to this function the implementation can use
141  *   any session functions.
142  */
TA_CloseSessionEntryPoint(void * session_context)143 void TA_CloseSessionEntryPoint(void* session_context)
144 {
145     (void)session_context;
146     tlogd("---- close session ----- ");
147 }
148 
149 /**
150  * Function TA_DestroyEntryPoint
151  * Description:
152  *   The function TA_DestroyEntryPoint is the Trusted Application's destructor,
153  *   which the Framework calls when the instance is being destroyed.
154  */
TA_DestroyEntryPoint(void)155 void TA_DestroyEntryPoint(void)
156 {
157     tlogd("---- destroy TA ---- ");
158 }
159