• 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 #include "tee_object_api.h"
13 #include <securec.h>
14 #include "tee_log.h"
15 #include "tee_obj.h"
16 #include "tee_core_api.h"
17 #include "tee_ss_agent_api.h"
18 #include "tee_obj_attr.h"
19 
20 #ifndef SUPPORT_GP_PANIC
21 #define TEE_Panic(x) \
22     do { \
23     } while (0)
24 #endif
25 
TEE_GetObjectInfo1(TEE_ObjectHandle object,TEE_ObjectInfo * objectInfo)26 TEE_Result TEE_GetObjectInfo1(
27     TEE_ObjectHandle object,
28     TEE_ObjectInfo *objectInfo)
29 {
30     uint32_t pos = 0;
31     uint32_t len = 0;
32     TEE_Result ret;
33     tlogd("TEE_GetObjectInfo1 start!\n");
34 
35     if (objectInfo == NULL || object == NULL) {
36         tloge("bad parameter!\n");
37         TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
38         return TEE_ERROR_BAD_PARAMETERS;
39     }
40     if (check_object(object) != TEE_SUCCESS) {
41         tloge("object is invalid\n");
42         TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
43         return TEE_ERROR_BAD_PARAMETERS;
44     }
45 
46     if (object->ObjectInfo == NULL) {
47         tloge("objectInfo in obj is invalid\n");
48         TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
49         return TEE_ERROR_BAD_PARAMETERS;
50     }
51 
52     if ((object->ObjectInfo->handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) {
53         ret = TEE_InfoObjectData(object, &pos, &len);
54         if (ret != TEE_SUCCESS) {
55             tloge("info object failed, ret=0x%x\n", ret);
56             TEE_Panic(TEE_ERROR_STORAGE_NOT_AVAILABLE);
57             return TEE_ERROR_STORAGE_NOT_AVAILABLE;
58         }
59         objectInfo->dataSize = len;
60         objectInfo->dataPosition = pos;
61     } else {
62         objectInfo->dataSize = 0;
63         objectInfo->dataPosition = 0;
64     }
65 
66     objectInfo->objectType = object->ObjectInfo->objectType;
67     objectInfo->objectSize = object->ObjectInfo->objectSize;
68     objectInfo->maxObjectSize = object->ObjectInfo->maxObjectSize;
69     objectInfo->objectUsage = object->ObjectInfo->objectUsage;
70     objectInfo->handleFlags = object->ObjectInfo->handleFlags;
71 
72     tlogd("TEE_GetObjectInfo1 end!\n");
73     return TEE_SUCCESS;
74 }
75 
TEE_RestrictObjectUsage1(TEE_ObjectHandle object,uint32_t objectUsage)76 TEE_Result TEE_RestrictObjectUsage1(
77     TEE_ObjectHandle  object,
78     uint32_t objectUsage)
79 {
80     tlogd("TEE_RestrictObjectUsage1 start!\n");
81 
82     if (object == NULL) {
83         tloge("bad parameter!\n");
84         return TEE_ERROR_BAD_PARAMETERS;
85     }
86     if (check_object(object) != TEE_SUCCESS) {
87         tloge("object is invalid\n");
88 #ifdef SUPPORT_GP_PANIC
89         TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
90 #endif
91         return TEE_ERROR_BAD_PARAMETERS;
92     }
93 
94     if (object->ObjectInfo == NULL) {
95         tloge("objectInfo in obj is invalid\n");
96 #ifdef SUPPORT_GP_PANIC
97         TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
98 #endif
99         return TEE_ERROR_BAD_PARAMETERS;
100     }
101     object->ObjectInfo->objectUsage = (object->ObjectInfo->objectUsage) & (objectUsage);
102 
103     return TEE_SUCCESS;
104 }
105 
get_obj_attr_param_check(TEE_ObjectHandle object,size_t * size)106 static TEE_Result get_obj_attr_param_check(TEE_ObjectHandle object, size_t *size)
107 {
108     /* Make sure the object is initialized */
109     if (object == NULL || size == NULL) {
110         tloge("bad parameter!\n");
111         return  TEE_ERROR_BAD_PARAMETERS;
112     }
113 
114     if (check_object(object) != TEE_SUCCESS) {
115         tloge("object is invalid\n");
116         return TEE_ERROR_BAD_PARAMETERS;
117     }
118 
119     if (object->ObjectInfo == NULL) {
120         tloge("objectInfo in obj is invalid\n");
121         return TEE_ERROR_BAD_PARAMETERS;
122     }
123 
124     if ((object->ObjectInfo->handleFlags & TEE_HANDLE_FLAG_INITIALIZED) != TEE_HANDLE_FLAG_INITIALIZED) {
125         tloge("obj not initialized\n");
126         return  TEE_ERROR_ITEM_NOT_FOUND;
127     }
128     return TEE_SUCCESS;
129 }
130 
TEE_GetObjectBufferAttribute(TEE_ObjectHandle object,uint32_t attributeID,void * buffer,size_t * size)131 TEE_Result TEE_GetObjectBufferAttribute(
132     TEE_ObjectHandle object,
133     uint32_t attributeID,
134     void *buffer, size_t *size)
135 {
136     void *src = NULL;
137     uint32_t attrc;
138     uint32_t i = 0;
139 
140     TEE_Result ret = get_obj_attr_param_check(object, size);
141     if (ret != TEE_SUCCESS)
142         return ret;
143 
144     /* public judge */
145     if (TEE_ATTR_IS_PROTECTED(attributeID)) {
146         if ((object->ObjectInfo->objectUsage & TEE_USAGE_EXTRACTABLE) == 0) {
147             tloge("Access denied\n");
148             return TEE_ERROR_ACCESS_DENIED;
149         }
150     }
151 
152     if (TEE_ATTR_IS_BUFFER(attributeID)) {
153         if (object->Attribute == NULL)
154             return TEE_ERROR_BAD_PARAMETERS;
155 
156         attrc = get_attr_count_for_object_type(object->ObjectInfo->objectType);
157         while (i < attrc) {
158             if (object->Attribute[i].attributeID != attributeID) {
159                 i++;
160                 continue;
161             }
162 
163             src = object->Attribute[i].content.ref.buffer;
164             if (src == NULL)
165                 return TEE_ERROR_BAD_STATE;
166 
167             if (buffer == NULL) {
168                 *size = object->Attribute[i].content.ref.length;
169                 return TEE_ERROR_SHORT_BUFFER;
170             }
171             if (*size < object->Attribute[i].content.ref.length) {
172                 tloge("buffer is too small\n");
173                 *size = object->Attribute[i].content.ref.length;
174                 return  TEE_ERROR_SHORT_BUFFER;
175             }
176             if (memmove_s(buffer, *size, src, object->Attribute[i].content.ref.length) != EOK)
177                 return TEE_ERROR_SECURITY;
178             *size = object->Attribute[i].content.ref.length;
179             return TEE_SUCCESS;
180         }
181         tloge("this attrbuteID is not exist\n");
182         return TEE_ERROR_ITEM_NOT_FOUND;
183     }
184     tloge("attributeID 29 bit is wrong\n");
185     return TEE_ERROR_BAD_PARAMETERS;
186 }
187 
TEE_GetObjectValueAttribute(TEE_ObjectHandle object,uint32_t attributeID,uint32_t * a,uint32_t * b)188 TEE_Result TEE_GetObjectValueAttribute(TEE_ObjectHandle object, uint32_t attributeID,
189     uint32_t *a, uint32_t *b)
190 {
191     uint32_t attrc;
192     uint32_t i = 0;
193     tlogd("TEE_GetObjectValueAttribute start!\n");
194 
195     /* Make sure the object is initialized */
196     if (object == NULL || (a == NULL && b == NULL)) {
197         tloge("bad parameter!\n");
198         return TEE_ERROR_BAD_PARAMETERS;
199     }
200 
201     if (check_object(object) != TEE_SUCCESS) {
202         tloge("object is invalid\n");
203         return TEE_ERROR_BAD_PARAMETERS;
204     }
205 
206     if (object->ObjectInfo == NULL) {
207         tloge("objectInfo in obj is invalid\n");
208         return TEE_ERROR_BAD_PARAMETERS;
209     }
210     if ((object->ObjectInfo->handleFlags &
211          TEE_HANDLE_FLAG_INITIALIZED) != TEE_HANDLE_FLAG_INITIALIZED) {
212         tloge("obj not initialized\n");
213         return TEE_ERROR_ITEM_NOT_FOUND;
214     }
215 
216     /* public judge */
217     if (TEE_ATTR_IS_PROTECTED(attributeID)) {
218         if ((object->ObjectInfo->objectUsage & TEE_USAGE_EXTRACTABLE) == 0) {
219             tloge("Access denied\n");
220             return TEE_ERROR_ACCESS_DENIED;
221         }
222     }
223     if (TEE_ATTR_IS_VALUE(attributeID)) {
224         if (object->Attribute == NULL)
225             return TEE_ERROR_BAD_PARAMETERS;
226         attrc = get_attr_count_for_object_type(object->ObjectInfo->objectType);
227         while (i < attrc) {
228             if (object->Attribute[i].attributeID != attributeID) {
229                 i++;
230                 continue;
231             }
232             if (a != NULL)
233                 *a = object->Attribute[i].content.value.a;
234             if (b != NULL)
235                 *b = object->Attribute[i].content.value.b;
236             return TEE_SUCCESS;
237         }
238         return TEE_ERROR_ITEM_NOT_FOUND;
239     }
240     tloge("attributeID 29 bit is wrong\n");
241     return TEE_ERROR_BAD_PARAMETERS;
242 }
243 
TEE_CloseObject(TEE_ObjectHandle object)244 void TEE_CloseObject(TEE_ObjectHandle object)
245 {
246     if (check_object_valid(object) != TEE_SUCCESS) {
247         tloge("object is invalid\n");
248         TEE_Panic(TEE_ERROR_BAD_PARAMETERS);
249         return;
250     }
251 
252     /* save objectinfo */
253     if ((object->ObjectInfo->handleFlags & TEE_HANDLE_FLAG_PERSISTENT) != 0) {
254         tlogd("this is a persistent object\n");
255         ss_agent_close_object(object);
256     } else {
257         tlogd("this is a transitent object\n");
258 
259         /* Make Persistent object to be transient object to use TEE_FreeTransientObject */
260         object->ObjectInfo->handleFlags &= (~TEE_HANDLE_FLAG_PERSISTENT);
261         TEE_FreeTransientObject(object);
262         tlogd("TEE_CloseObject end!\n");
263         return;
264     }
265 }
266