1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "asset_system_api.h"
17 
18 #include "securec.h"
19 
20 #include "asset_log.h"
21 #include "asset_mem.h"
22 
23 int32_t add_asset(const AssetAttr *attributes, uint32_t attr_cnt);
24 int32_t remove_asset(const AssetAttr *query, uint32_t query_cnt);
25 int32_t update_asset(const AssetAttr *query, uint32_t query_cnt,
26     const AssetAttr *attributes_to_update, uint32_t update_cnt);
27 int32_t pre_query_asset(const AssetAttr *query, uint32_t query_cnt, AssetBlob *challenge);
28 int32_t query_asset(const AssetAttr *query, uint32_t query_cnt, AssetResultSet *result_set);
29 int32_t post_query_asset(const AssetAttr *handle, uint32_t handle_cnt);
30 
AssetAdd(const AssetAttr * attributes,uint32_t attrCnt)31 int32_t AssetAdd(const AssetAttr *attributes, uint32_t attrCnt)
32 {
33     return add_asset(attributes, attrCnt);
34 }
35 
AssetRemove(const AssetAttr * query,uint32_t queryCnt)36 int32_t AssetRemove(const AssetAttr *query, uint32_t queryCnt)
37 {
38     return remove_asset(query, queryCnt);
39 }
40 
AssetUpdate(const AssetAttr * query,uint32_t queryCnt,const AssetAttr * attributesToUpdate,uint32_t updateCnt)41 int32_t AssetUpdate(const AssetAttr *query, uint32_t queryCnt,
42     const AssetAttr *attributesToUpdate, uint32_t updateCnt)
43 {
44     return update_asset(query, queryCnt, attributesToUpdate, updateCnt);
45 }
46 
AssetPreQuery(const AssetAttr * query,uint32_t queryCnt,AssetBlob * challenge)47 int32_t AssetPreQuery(const AssetAttr *query, uint32_t queryCnt, AssetBlob *challenge)
48 {
49     return pre_query_asset(query, queryCnt, challenge);
50 }
51 
AssetQuery(const AssetAttr * query,uint32_t queryCnt,AssetResultSet * resultSet)52 int32_t AssetQuery(const AssetAttr *query, uint32_t queryCnt, AssetResultSet *resultSet)
53 {
54     return query_asset(query, queryCnt, resultSet);
55 }
56 
AssetPostQuery(const AssetAttr * handle,uint32_t handleCnt)57 int32_t AssetPostQuery(const AssetAttr *handle, uint32_t handleCnt)
58 {
59     return post_query_asset(handle, handleCnt);
60 }
61 
AssetParseAttr(const AssetResult * result,AssetTag tag)62 AssetAttr *AssetParseAttr(const AssetResult *result, AssetTag tag)
63 {
64     if (result == NULL || result->attrs == NULL || result->count == 0) {
65         LOGE("[FATAL][SDK]Argument is NULL.");
66         return NULL;
67     }
68     for (uint32_t i = 0; i < result->count; i++) {
69         if (result->attrs[i].tag == tag) {
70             return &result->attrs[i];
71         }
72     }
73     LOGE("[FATAL][SDK]Attribute not found.");
74     return NULL;
75 }
76 
AssetFreeBlob(AssetBlob * blob)77 void AssetFreeBlob(AssetBlob *blob)
78 {
79     if (blob == NULL || blob->data == NULL || blob->size == 0) {
80         return;
81     }
82     (void)memset_s(blob->data, blob->size, 0, blob->size);
83     AssetFree(blob->data);
84     blob->data = NULL;
85     blob->size = 0;
86 }
87 
AssetFreeResultSet(AssetResultSet * resultSet)88 void AssetFreeResultSet(AssetResultSet *resultSet)
89 {
90     if (resultSet == NULL || resultSet->results == NULL || resultSet->count == 0) {
91         return;
92     }
93 
94     for (uint32_t i = 0; i < resultSet->count; i++) {
95         AssetAttr *attrs = resultSet->results[i].attrs;
96         uint32_t attrCnt = resultSet->results[i].count;
97         if (attrs == NULL || attrCnt == 0) {
98             continue;
99         }
100         for (uint32_t j = 0; j < attrCnt; j++) {
101             if ((attrs[j].tag & SEC_ASSET_TAG_TYPE_MASK) == SEC_ASSET_TYPE_BYTES) {
102                 AssetFreeBlob(&attrs[j].value.blob);
103             }
104         }
105         AssetFree(resultSet->results[i].attrs);
106         resultSet->results[i].attrs = NULL;
107         resultSet->results[i].count = 0;
108     }
109     AssetFree(resultSet->results);
110     resultSet->results = NULL;
111     resultSet->count = 0;
112 }