• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_pre_query.h"
17 
18 #include <cstdint>
19 #include <vector>
20 
21 #include "securec.h"
22 
23 #include "asset_log.h"
24 #include "asset_system_api.h"
25 #include "asset_system_type.h"
26 
27 #include "asset_napi_check.h"
28 #include "asset_napi_common.h"
29 
30 namespace OHOS {
31 namespace Security {
32 namespace Asset {
33 namespace {
34 const uint32_t QUERY_ARG_COUNT = 1;
35 const uint32_t QUERY_ARG_COUNT_AS_USER = 2;
36 
37 const std::vector<uint32_t> OPTIONAL_TAGS = {
38     SEC_ASSET_TAG_AUTH_VALIDITY_PERIOD
39 };
40 
CheckPreQueryArgs(const napi_env env,const std::vector<AssetAttr> & attrs)41 napi_status CheckPreQueryArgs(const napi_env env, const std::vector<AssetAttr> &attrs)
42 {
43     std::vector<uint32_t> validTags;
44     validTags.insert(validTags.end(), CRITICAL_LABEL_TAGS.begin(), CRITICAL_LABEL_TAGS.end());
45     validTags.insert(validTags.end(), NORMAL_LABEL_TAGS.begin(), NORMAL_LABEL_TAGS.end());
46     validTags.insert(validTags.end(), NORMAL_LOCAL_LABEL_TAGS.begin(), NORMAL_LOCAL_LABEL_TAGS.end());
47     validTags.insert(validTags.end(), ACCESS_CONTROL_TAGS.begin(), ACCESS_CONTROL_TAGS.end());
48     validTags.insert(validTags.end(), OPTIONAL_TAGS.begin(), OPTIONAL_TAGS.end());
49     IF_ERROR_THROW_RETURN(env, CheckAssetTagValidity(env, attrs, validTags, SEC_ASSET_INVALID_ARGUMENT));
50     IF_ERROR_THROW_RETURN(env, CheckAssetValueValidity(env, attrs, SEC_ASSET_INVALID_ARGUMENT));
51     return napi_ok;
52 }
53 
ParseAttrMap(napi_env env,napi_callback_info info,BaseContext * context)54 napi_status ParseAttrMap(napi_env env, napi_callback_info info, BaseContext *context)
55 {
56     napi_value argv[MAX_ARGS_NUM] = { 0 };
57     IF_ERR_RETURN(ParseJsArgs(env, info, argv, QUERY_ARG_COUNT));
58     IF_ERR_RETURN(ParseJsMap(env, argv[0], context->attrs));
59     IF_ERR_RETURN(CheckPreQueryArgs(env, context->attrs));
60     return napi_ok;
61 }
62 
ParseAttrMapAsUser(napi_env env,napi_callback_info info,BaseContext * context)63 napi_status ParseAttrMapAsUser(napi_env env, napi_callback_info info, BaseContext *context)
64 {
65     napi_value argv[MAX_ARGS_NUM] = { 0 };
66     IF_ERR_RETURN(ParseJsArgs(env, info, argv, QUERY_ARG_COUNT_AS_USER));
67     uint32_t index = 0;
68     IF_ERR_RETURN(ParseJsUserId(env, argv[index++], context->attrs));
69     IF_ERR_RETURN(ParseJsMap(env, argv[index++], context->attrs));
70     IF_ERR_RETURN(CheckPreQueryArgs(env, context->attrs));
71     return napi_ok;
72 }
73 } // anonymous namespace
74 
NapiPreQuery(const napi_env env,napi_callback_info info,bool asUser,bool async)75 napi_value NapiPreQuery(const napi_env env, napi_callback_info info, bool asUser, bool async)
76 {
77     auto context = std::unique_ptr<PreQueryContext>(new (std::nothrow)PreQueryContext());
78     NAPI_THROW(env, context == nullptr, SEC_ASSET_OUT_OF_MEMORY, "Unable to allocate memory for Context.");
79 
80     context->parse = asUser ? ParseAttrMapAsUser : ParseAttrMap;
81     context->execute = [](napi_env env, void *data) {
82         PreQueryContext *context = static_cast<PreQueryContext *>(data);
83         context->result = AssetPreQuery(&context->attrs[0], context->attrs.size(), &context->challenge);
84     };
85 
86     context->resolve = [](napi_env env, BaseContext *baseContext) -> napi_value {
87         PreQueryContext *context = static_cast<PreQueryContext *>(baseContext);
88         return CreateJsUint8Array(env, context->challenge);
89     };
90 
91     if (async) {
92         return CreateAsyncWork(env, info, std::move(context), __func__);
93     } else {
94         return CreateSyncWork(env, info, context.get());
95     }
96 }
97 
NapiPreQuery(const napi_env env,napi_callback_info info)98 napi_value NapiPreQuery(const napi_env env, napi_callback_info info)
99 {
100     return NapiPreQuery(env, info, false, true);
101 }
102 
NapiPreQueryAsUser(const napi_env env,napi_callback_info info)103 napi_value NapiPreQueryAsUser(const napi_env env, napi_callback_info info)
104 {
105     return NapiPreQuery(env, info, true, true);
106 }
107 
NapiPreQuerySync(const napi_env env,napi_callback_info info)108 napi_value NapiPreQuerySync(const napi_env env, napi_callback_info info)
109 {
110     return NapiPreQuery(env, info, false, false);
111 }
112 
113 } // Asset
114 } // Security
115 } // OHOS
116