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 "string.h"
13 #include "tee_log.h"
14 #include "tee_ss_agent_api.h"
15 #include "sfs.h"
16 #include "ssa_helper.h"
17
create_param_mapping(const union ssa_agent_msg * msg,uint32_t sndr,mem_map_info_t * obj_id_info,mem_map_info_t * attributes_info,mem_map_info_t * initial_data)18 TEE_Result create_param_mapping(const union ssa_agent_msg *msg, uint32_t sndr, mem_map_info_t *obj_id_info,
19 mem_map_info_t *attributes_info, mem_map_info_t *initial_data)
20 {
21 if (msg == NULL || obj_id_info == NULL || attributes_info == NULL || initial_data == NULL ||
22 msg->create_obj.object_id == 0)
23 return TEE_ERROR_BAD_PARAMETERS;
24
25 obj_id_info->vm_addr = 0;
26 obj_id_info->size = msg->create_obj.obj_id_len;
27 obj_id_info->mapped = false;
28 attributes_info->vm_addr = 0;
29 attributes_info->size = msg->create_obj.attributes_len;
30 attributes_info->mapped = false;
31 initial_data->vm_addr = 0;
32 initial_data->size = msg->create_obj.data_len;
33 initial_data->mapped = false;
34
35 if (ssa_map_from_task(sndr, msg->create_obj.object_id, msg->create_obj.obj_id_len, g_ssagent_handle,
36 &obj_id_info->vm_addr) != 0) {
37 tloge("map objectID from 0x%x fail\n", sndr);
38 goto clean;
39 }
40 obj_id_info->mapped = true;
41
42 bool attr_is_valid = (msg->create_obj.attributes != 0) && (msg->create_obj.attributes_len != 0);
43 if (attr_is_valid) {
44 if (ssa_map_from_task(sndr, msg->create_obj.attributes, msg->create_obj.attributes_len, g_ssagent_handle,
45 &attributes_info->vm_addr) != 0) {
46 tloge("map attributes from 0x%x fail\n", sndr);
47 goto clean;
48 }
49 attributes_info->mapped = true;
50 }
51
52 bool initial_data_valid = (msg->create_obj.initial_data != 0) && (msg->create_obj.data_len != 0);
53 if (initial_data_valid) {
54 if (ssa_map_from_task(sndr, msg->create_obj.initial_data, msg->create_obj.data_len, g_ssagent_handle,
55 &initial_data->vm_addr) != 0) {
56 tloge("map initialData from 0x%x fail\n", sndr);
57 goto clean;
58 }
59 initial_data->mapped = true;
60 }
61
62 return TEE_SUCCESS;
63 clean:
64 create_param_unmapping(obj_id_info, attributes_info, initial_data);
65 return TEE_ERROR_GENERIC;
66 }
67
create_param_unmapping(const mem_map_info_t * obj_id_info,const mem_map_info_t * attributes_info,const mem_map_info_t * initial_data)68 void create_param_unmapping(const mem_map_info_t *obj_id_info, const mem_map_info_t *attributes_info,
69 const mem_map_info_t *initial_data)
70 {
71 if (obj_id_info != NULL)
72 ssa_unmap_from_task(g_ssagent_handle, obj_id_info->vm_addr, obj_id_info->size, obj_id_info->mapped);
73
74 if (attributes_info != NULL)
75 ssa_unmap_from_task(g_ssagent_handle, attributes_info->vm_addr, attributes_info->size, attributes_info->mapped);
76
77 if (initial_data != NULL)
78 ssa_unmap_from_task(g_ssagent_handle, initial_data->vm_addr, initial_data->size, initial_data->mapped);
79 }
80
create_object_proc(const struct create_obj_msg_t * create_obj,uint32_t sndr,const TEE_UUID * uuid,struct ssa_agent_rsp * rsp)81 void create_object_proc(const struct create_obj_msg_t *create_obj, uint32_t sndr,
82 const TEE_UUID *uuid, struct ssa_agent_rsp *rsp)
83 {
84 struct sfd_t *sfd = NULL;
85 TEE_Result error = TEE_ERROR_GENERIC;
86 uint32_t obj = 0;
87
88 if (rsp == NULL)
89 return;
90
91 if (create_obj == NULL || uuid == NULL) {
92 rsp->ret = TEE_ERROR_BAD_PARAMETERS;
93 return;
94 }
95
96 create_object(create_obj, sndr, uuid, &sfd, &obj, &error);
97 if (error != TEE_SUCCESS) {
98 rsp->ret = error;
99 return;
100 }
101
102 rsp->ret = TEE_SUCCESS;
103 rsp->create_obj.obj_index = obj;
104 /* update new_seek_pos if has initial data */
105 rsp->create_obj.new_seek_pos = 0; /* The initial data position in the data stream is set to 0 */
106 rsp->create_obj.new_size = sfd->size - sfd->attr_size;
107 tlogd("obj %u created\n", rsp->create_obj.obj_index);
108 }
109
open_param_mapping(const union ssa_agent_msg * msg,uint32_t sndr,mem_map_info_t * obj_id_info,mem_map_info_t * attributes_info)110 TEE_Result open_param_mapping(const union ssa_agent_msg *msg, uint32_t sndr,
111 mem_map_info_t *obj_id_info, mem_map_info_t *attributes_info)
112 {
113 if (msg == NULL || obj_id_info == NULL || attributes_info == NULL || msg->open_obj.object_id == 0)
114 return TEE_ERROR_BAD_PARAMETERS;
115
116 obj_id_info->vm_addr = 0;
117 obj_id_info->size = msg->open_obj.obj_id_len;
118 obj_id_info->mapped = false;
119 attributes_info->vm_addr = 0;
120 attributes_info->size = msg->open_obj.attr_head_size;
121 attributes_info->mapped = false;
122
123 if (ssa_map_from_task(sndr, msg->open_obj.object_id, msg->open_obj.obj_id_len,
124 g_ssagent_handle, &obj_id_info->vm_addr) != 0) {
125 tloge("map objectID from 0x%x fail\n", sndr);
126 goto clean;
127 }
128 obj_id_info->mapped = true;
129
130 if ((msg->open_obj.attr_head != 0) && (msg->open_obj.attr_head_size != 0)) {
131 if (ssa_map_from_task(sndr, msg->open_obj.attr_head, msg->open_obj.attr_head_size, g_ssagent_handle,
132 &attributes_info->vm_addr) != 0) {
133 tloge("map attributes from 0x%x fail\n", sndr);
134 goto clean;
135 }
136 attributes_info->mapped = true;
137 }
138
139 return TEE_SUCCESS;
140
141 clean:
142 open_param_unmapping(obj_id_info, attributes_info);
143 return TEE_ERROR_GENERIC;
144 }
145
open_param_unmapping(const mem_map_info_t * obj_id_info,const mem_map_info_t * attributes_info)146 void open_param_unmapping(const mem_map_info_t *obj_id_info, const mem_map_info_t *attributes_info)
147 {
148 if (obj_id_info != NULL)
149 ssa_unmap_from_task(g_ssagent_handle, obj_id_info->vm_addr, obj_id_info->size, obj_id_info->mapped);
150
151 if (attributes_info != NULL)
152 ssa_unmap_from_task(g_ssagent_handle, attributes_info->vm_addr, attributes_info->size, attributes_info->mapped);
153 }
154
ssa_internal_fcreate(const char * file_name,const TEE_UUID * uuid,struct sfd_t ** sfd)155 TEE_Result ssa_internal_fcreate(const char *file_name, const TEE_UUID *uuid, struct sfd_t **sfd)
156 {
157 meta_data_t *meta = NULL;
158 TEE_Result ret = TEE_ERROR_GENERIC;
159
160 if (file_name == NULL || sfd == NULL)
161 return TEE_ERROR_BAD_PARAMETERS;
162
163 meta = create_meta_data((uint8_t *)file_name, strlen(file_name), TEE_OBJECT_STORAGE_PRIVATE,
164 TA_KEY_COMPOSED_OF_TWO_16BYTES_KEYS, uuid, &ret, SFS_ARCH_VERSION_SSA);
165 if (meta == NULL) {
166 tloge("meta data create fail\n");
167 return TEE_ERROR_GENERIC;
168 }
169
170 *sfd = ssa_create(meta, TEE_DATA_FLAG_ACCESS_WRITE, &ret);
171 if (*sfd == NULL) {
172 tloge("create fail ret=0x%x\n", ret);
173 goto clean;
174 }
175
176 return TEE_SUCCESS;
177
178 clean:
179 free_meta_data(&meta);
180 return ret;
181 }
182
ssa_internal_fopen(const char * file_name,const TEE_UUID * uuid,struct sfd_t ** sfd)183 TEE_Result ssa_internal_fopen(const char *file_name, const TEE_UUID *uuid, struct sfd_t **sfd)
184 {
185 meta_data_t *meta = NULL;
186 TEE_Result ret = TEE_ERROR_GENERIC;
187
188 if (file_name == NULL || sfd == NULL)
189 return TEE_ERROR_BAD_PARAMETERS;
190
191 meta = create_meta_data((uint8_t *)file_name, strlen(file_name), TEE_OBJECT_STORAGE_PRIVATE,
192 TA_KEY_COMPOSED_OF_TWO_16BYTES_KEYS, uuid, &ret, SFS_ARCH_VERSION_SSA);
193 if (meta == NULL) {
194 tloge("meta data create fail\n");
195 ret = TEE_ERROR_GENERIC;
196 return ret;
197 }
198
199 *sfd = ssa_open(meta, TEE_DATA_FLAG_ACCESS_READ | TEE_DATA_FLAG_ACCESS_WRITE, &ret);
200 if (*sfd == NULL) {
201 tloge("open fail ret=0x%x\n", ret);
202 goto clean;
203 }
204
205 return TEE_SUCCESS;
206
207 clean:
208 free_meta_data(&meta);
209 return ret;
210 }
211
ssa_internal_fwrite(struct sfd_t * sfd,const uint8_t * in_buff,uint32_t len)212 uint32_t ssa_internal_fwrite(struct sfd_t *sfd, const uint8_t *in_buff, uint32_t len)
213 {
214 TEE_Result ret;
215 uint32_t count;
216
217 if (sfd == NULL || in_buff == NULL)
218 return 0;
219
220 ret = ssa_seek(sfd, 0, TEE_DATA_SEEK_SET);
221 if (ret != TEE_SUCCESS) {
222 tloge("seek file failed ret=0x%x\n", ret);
223 return 0;
224 }
225
226 count = ssa_write(in_buff, len, sfd, &ret);
227 if (ret != TEE_SUCCESS || count != len) {
228 tloge("ssa write fail, ret=%x", ret);
229 return 0;
230 }
231
232 ret = ssa_truncate(sfd, count);
233 if (ret != TEE_SUCCESS) {
234 tloge("truncate error, ret:%x", ret);
235 return ret;
236 }
237
238 sfd->need_update_hmac = false;
239 ret = ssa_write_mac(sfd);
240 if (ret != TEE_SUCCESS) {
241 tloge("write mac fail %x", ret);
242 return 0;
243 }
244
245 return count;
246 }
247
ssa_internal_fclose(struct sfd_t * sfd)248 void ssa_internal_fclose(struct sfd_t *sfd)
249 {
250 TEE_Result ret;
251 meta_data_t *meta = NULL;
252
253 if (sfd == NULL)
254 return;
255
256 meta = sfd->meta_data;
257
258 ret = ssa_close(sfd);
259 sfd = NULL;
260 if (ret != TEE_SUCCESS)
261 tloge("close file failed\n");
262
263 free_meta_data(&meta);
264 return;
265 }
266
ssa_internal_fremove(struct sfd_t * sfd)267 void ssa_internal_fremove(struct sfd_t *sfd)
268 {
269 meta_data_t *meta = NULL;
270
271 if (sfd == NULL)
272 return;
273
274 meta = sfd->meta_data;
275 (void)ssa_close_and_delete(sfd, true);
276 sfd = NULL;
277 free_meta_data(&meta);
278
279 return;
280 }
281