1 /*
2 * Copyright (c) 2024-2025 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_napi_update.h"
17
18 #include "securec.h"
19
20 #include "asset_log.h"
21 #include "asset_system_api.h"
22 #include "asset_system_type.h"
23
24 #include "asset_napi_check.h"
25 #include "asset_napi_common.h"
26
27 namespace OHOS {
28 namespace Security {
29 namespace Asset {
30 namespace {
31 const uint32_t UPDATE_ARG_COUNT = 2;
32 const uint32_t UPDATE_ARG_COUNT_AS_USER = 3;
33
34 const std::vector<uint32_t> QUERY_REQUIRED_TAGS = {
35 SEC_ASSET_TAG_ALIAS
36 };
37
38 const std::vector<uint32_t> UPDATE_OPTIONAL_TAGS = {
39 SEC_ASSET_TAG_SECRET
40 };
41
CheckAssetPresence(const napi_env env,const std::vector<AssetAttr> & attrs)42 napi_value CheckAssetPresence(const napi_env env, const std::vector<AssetAttr> &attrs)
43 {
44 if (attrs.empty()) {
45 RETURN_JS_ERROR(env, SEC_ASSET_INVALID_ARGUMENT, "Argument[attributesToUpdate] is empty.");
46 }
47 return nullptr;
48 }
49
CheckUpdateArgs(const napi_env env,const std::vector<AssetAttr> & attrs,const std::vector<AssetAttr> & updateAttrs)50 napi_status CheckUpdateArgs(const napi_env env, const std::vector<AssetAttr> &attrs,
51 const std::vector<AssetAttr> &updateAttrs)
52 {
53 IF_ERROR_THROW_RETURN(env, CheckAssetRequiredTag(env, attrs, QUERY_REQUIRED_TAGS, SEC_ASSET_INVALID_ARGUMENT));
54 std::vector<uint32_t> queryValidTags;
55 queryValidTags.insert(queryValidTags.end(), CRITICAL_LABEL_TAGS.begin(), CRITICAL_LABEL_TAGS.end());
56 queryValidTags.insert(queryValidTags.end(), NORMAL_LABEL_TAGS.begin(), NORMAL_LABEL_TAGS.end());
57 queryValidTags.insert(queryValidTags.end(), NORMAL_LOCAL_LABEL_TAGS.begin(), NORMAL_LOCAL_LABEL_TAGS.end());
58 queryValidTags.insert(queryValidTags.end(), ACCESS_CONTROL_TAGS.begin(), ACCESS_CONTROL_TAGS.end());
59 IF_ERROR_THROW_RETURN(env, CheckAssetTagValidity(env, attrs, queryValidTags, SEC_ASSET_INVALID_ARGUMENT));
60 IF_ERROR_THROW_RETURN(env, CheckAssetValueValidity(env, attrs, SEC_ASSET_INVALID_ARGUMENT));
61
62 IF_ERROR_THROW_RETURN(env, CheckAssetPresence(env, updateAttrs));
63 std::vector<uint32_t> updateValidTags;
64 updateValidTags.insert(updateValidTags.end(), NORMAL_LABEL_TAGS.begin(), NORMAL_LABEL_TAGS.end());
65 updateValidTags.insert(updateValidTags.end(), NORMAL_LOCAL_LABEL_TAGS.begin(), NORMAL_LOCAL_LABEL_TAGS.end());
66 updateValidTags.insert(updateValidTags.end(), ASSET_SYNC_TAGS.begin(), ASSET_SYNC_TAGS.end());
67 updateValidTags.insert(updateValidTags.end(), UPDATE_OPTIONAL_TAGS.begin(), UPDATE_OPTIONAL_TAGS.end());
68 IF_ERROR_THROW_RETURN(env, CheckAssetTagValidity(env, updateAttrs, updateValidTags, SEC_ASSET_INVALID_ARGUMENT));
69 IF_ERROR_THROW_RETURN(env, CheckAssetValueValidity(env, updateAttrs, SEC_ASSET_INVALID_ARGUMENT));
70
71 return napi_ok;
72 }
73
ParseAttrMap(napi_env env,napi_callback_info info,BaseContext * baseContext)74 napi_status ParseAttrMap(napi_env env, napi_callback_info info, BaseContext *baseContext)
75 {
76 UpdateContext *context = reinterpret_cast<UpdateContext *>(baseContext);
77 napi_value argv[MAX_ARGS_NUM] = { 0 };
78 IF_ERR_RETURN(ParseJsArgs(env, info, argv, UPDATE_ARG_COUNT));
79 uint32_t index = 0;
80 IF_ERR_RETURN(ParseJsMap(env, argv[index++], context->attrs));
81 IF_ERR_RETURN(ParseJsMap(env, argv[index++], context->updateAttrs));
82 IF_ERR_RETURN(CheckUpdateArgs(env, context->attrs, context->updateAttrs));
83 return napi_ok;
84 }
85
ParseAttrMapAsUser(napi_env env,napi_callback_info info,BaseContext * baseContext)86 napi_status ParseAttrMapAsUser(napi_env env, napi_callback_info info, BaseContext *baseContext)
87 {
88 UpdateContext *context = reinterpret_cast<UpdateContext *>(baseContext);
89 napi_value argv[MAX_ARGS_NUM] = { 0 };
90 IF_ERR_RETURN(ParseJsArgs(env, info, argv, UPDATE_ARG_COUNT_AS_USER));
91 uint32_t index = 0;
92 IF_ERR_RETURN(ParseJsUserId(env, argv[index++], context->attrs));
93 IF_ERR_RETURN(ParseJsMap(env, argv[index++], context->attrs));
94 IF_ERR_RETURN(ParseJsMap(env, argv[index++], context->updateAttrs));
95 IF_ERR_RETURN(CheckUpdateArgs(env, context->attrs, context->updateAttrs));
96 return napi_ok;
97 }
98 } // anonymous namespace
99
NapiUpdate(const napi_env env,napi_callback_info info,bool asUser,bool async)100 napi_value NapiUpdate(const napi_env env, napi_callback_info info, bool asUser, bool async)
101 {
102 auto context = std::unique_ptr<UpdateContext>(new (std::nothrow)UpdateContext());
103 NAPI_THROW(env, context == nullptr, SEC_ASSET_OUT_OF_MEMORY, "Unable to allocate memory for Context.");
104
105 context->parse = asUser ? ParseAttrMapAsUser : ParseAttrMap;
106 context->execute = [](napi_env env, void *data) {
107 UpdateContext *context = static_cast<UpdateContext *>(data);
108 context->result = AssetUpdate(&context->attrs[0], context->attrs.size(),
109 &context->updateAttrs[0], context->updateAttrs.size());
110 };
111
112 context->resolve = [](napi_env env, BaseContext *context) -> napi_value {
113 return CreateJsUndefined(env);
114 };
115
116 if (async) {
117 return CreateAsyncWork(env, info, std::move(context), __func__);
118 } else {
119 return CreateSyncWork(env, info, context.get());
120 }
121 }
122
NapiUpdate(const napi_env env,napi_callback_info info)123 napi_value NapiUpdate(const napi_env env, napi_callback_info info)
124 {
125 return NapiUpdate(env, info, false, true);
126 }
127
NapiUpdateAsUser(const napi_env env,napi_callback_info info)128 napi_value NapiUpdateAsUser(const napi_env env, napi_callback_info info)
129 {
130 return NapiUpdate(env, info, true, true);
131 }
132
NapiUpdateSync(const napi_env env,napi_callback_info info)133 napi_value NapiUpdateSync(const napi_env env, napi_callback_info info)
134 {
135 return NapiUpdate(env, info, false, false);
136 }
137
138 } // Asset
139 } // Security
140 } // OHOS
141