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 <cstdint>
17 #include <vector>
18
19 #include "securec.h"
20
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23
24 #include "asset_log.h"
25 #include "asset_mem.h"
26 #include "asset_system_api.h"
27 #include "asset_system_type.h"
28
29 #include "asset_napi_check.h"
30 #include "asset_napi_common.h"
31 #include "asset_napi_update.h"
32
33 namespace OHOS {
34 namespace Security {
35 namespace Asset {
36 namespace {
37
38 const std::vector<uint32_t> QUERY_REQUIRED_TAGS = {
39 SEC_ASSET_TAG_ALIAS
40 };
41
42 const std::vector<uint32_t> UPDATE_OPTIONAL_TAGS = {
43 SEC_ASSET_TAG_SECRET
44 };
45
CheckAssetPresence(const napi_env env,const std::vector<AssetAttr> & attrs)46 bool CheckAssetPresence(const napi_env env, const std::vector<AssetAttr> &attrs)
47 {
48 if (attrs.empty()) {
49 NAPI_THROW_INVALID_ARGUMENT(env, "Argument[attributesToUpdate] is empty.");
50 return false;
51 }
52 return true;
53 }
54
CheckUpdateArgs(const napi_env env,const std::vector<AssetAttr> & attrs,const std::vector<AssetAttr> & updateAttrs)55 napi_status CheckUpdateArgs(const napi_env env, const std::vector<AssetAttr> &attrs,
56 const std::vector<AssetAttr> &updateAttrs)
57 {
58 IF_FALSE_RETURN(CheckAssetRequiredTag(env, attrs, QUERY_REQUIRED_TAGS), napi_invalid_arg);
59 std::vector<uint32_t> queryValidTags;
60 queryValidTags.insert(queryValidTags.end(), CRITICAL_LABEL_TAGS.begin(), CRITICAL_LABEL_TAGS.end());
61 queryValidTags.insert(queryValidTags.end(), NORMAL_LABEL_TAGS.begin(), NORMAL_LABEL_TAGS.end());
62 queryValidTags.insert(queryValidTags.end(), NORMAL_LOCAL_LABEL_TAGS.begin(), NORMAL_LOCAL_LABEL_TAGS.end());
63 queryValidTags.insert(queryValidTags.end(), ACCESS_CONTROL_TAGS.begin(), ACCESS_CONTROL_TAGS.end());
64 IF_FALSE_RETURN(CheckAssetTagValidity(env, attrs, queryValidTags), napi_invalid_arg);
65 IF_FALSE_RETURN(CheckAssetValueValidity(env, attrs), napi_invalid_arg);
66
67 IF_FALSE_RETURN(CheckAssetPresence(env, updateAttrs), napi_invalid_arg);
68 std::vector<uint32_t> updateValidTags;
69 updateValidTags.insert(updateValidTags.end(), NORMAL_LABEL_TAGS.begin(), NORMAL_LABEL_TAGS.end());
70 updateValidTags.insert(updateValidTags.end(), NORMAL_LOCAL_LABEL_TAGS.begin(), NORMAL_LOCAL_LABEL_TAGS.end());
71 updateValidTags.insert(updateValidTags.end(), ASSET_SYNC_TAGS.begin(), ASSET_SYNC_TAGS.end());
72 updateValidTags.insert(updateValidTags.end(), UPDATE_OPTIONAL_TAGS.begin(), UPDATE_OPTIONAL_TAGS.end());
73 IF_FALSE_RETURN(CheckAssetTagValidity(env, updateAttrs, updateValidTags), napi_invalid_arg);
74 IF_FALSE_RETURN(CheckAssetValueValidity(env, updateAttrs), napi_invalid_arg);
75
76 return napi_ok;
77 }
78
NapiUpdateAsync(const napi_env env,napi_callback_info info,const char * funcName,napi_async_execute_callback execute,const NapiCallerArgs & args)79 napi_value NapiUpdateAsync(const napi_env env, napi_callback_info info, const char *funcName,
80 napi_async_execute_callback execute, const NapiCallerArgs &args)
81 {
82 AsyncContext *context = CreateAsyncContext();
83 NAPI_THROW(env, context == nullptr, SEC_ASSET_OUT_OF_MEMORY, "Unable to allocate memory for AsyncContext.");
84
85 do {
86 if (ParseParam(env, info, args, context->attrs, context->updateAttrs) != napi_ok) {
87 break;
88 }
89
90 if (CheckUpdateArgs(env, context->attrs, context->updateAttrs) != napi_ok) {
91 break;
92 }
93
94 napi_value promise = CreateAsyncWork(env, context, funcName, execute);
95 if (promise == nullptr) {
96 LOGE("Create async work failed.");
97 break;
98 }
99 return promise;
100 } while (0);
101 DestroyAsyncContext(env, context);
102 return nullptr;
103 }
104
105 } // anonymous namespace
106
NapiUpdate(const napi_env env,napi_callback_info info,const NapiCallerArgs & args)107 napi_value NapiUpdate(const napi_env env, napi_callback_info info, const NapiCallerArgs &args)
108 {
109 napi_async_execute_callback execute =
110 [](napi_env env, void *data) {
111 AsyncContext *context = static_cast<AsyncContext *>(data);
112 context->result = AssetUpdate(&context->attrs[0], context->attrs.size(),
113 &context->updateAttrs[0], context->updateAttrs.size());
114 };
115 return NapiUpdateAsync(env, info, __func__, execute, args);
116 }
117
NapiUpdate(const napi_env env,napi_callback_info info)118 napi_value NapiUpdate(const napi_env env, napi_callback_info info)
119 {
120 NapiCallerArgs args = { .expectArgNum = UPDATE_ARGS_NUM, .isUpdate = true, .isAsUser = false };
121 return NapiUpdate(env, info, args);
122 }
123
NapiUpdateAsUser(const napi_env env,napi_callback_info info)124 napi_value NapiUpdateAsUser(const napi_env env, napi_callback_info info)
125 {
126 NapiCallerArgs args = { .expectArgNum = AS_USER_UPDATE_ARGS_NUM, .isUpdate = true, .isAsUser = true };
127 return NapiUpdate(env, info, args);
128 }
129
NapiUpdateSync(const napi_env env,napi_callback_info info)130 napi_value NapiUpdateSync(const napi_env env, napi_callback_info info)
131 {
132 std::vector<AssetAttr> attrs;
133 std::vector<AssetAttr> updateAttrs;
134 NapiCallerArgs args = { .expectArgNum = UPDATE_ARGS_NUM, .isUpdate = true, .isAsUser = false };
135 do {
136 if (ParseParam(env, info, args, attrs, updateAttrs) != napi_ok) {
137 break;
138 }
139
140 if (CheckUpdateArgs(env, attrs, updateAttrs) != napi_ok) {
141 break;
142 }
143
144 int32_t result = AssetUpdate(&attrs[0], attrs.size(), &updateAttrs[0], updateAttrs.size());
145 CHECK_RESULT_BREAK(env, result);
146 } while (false);
147 FreeAssetAttrs(attrs);
148 FreeAssetAttrs(updateAttrs);
149 return nullptr;
150 }
151
152 } // Asset
153 } // Security
154 } // OHOS
155