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 <algorithm>
17 #include <climits>
18 #include <cmath>
19 #include <functional>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "securec.h"
24 
25 #include "asset_log.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 
32 namespace OHOS {
33 namespace Security {
34 namespace Asset {
35 namespace {
36 
37 #define MIN_ARRAY_SIZE 0
38 #define MAX_SECRET_SIZE 1024
39 #define MAX_ALIAS_SIZE 256
40 #define MIN_NUMBER_VALUE 0
41 #define MAX_AUTH_VALID_PERIOD 600
42 #define CHALLENGE_SIZE 32
43 #define AUTH_TOKEN_SIZE 280
44 #define MAX_LABEL_SIZE 2048
45 #define MAX_RETURN_LIMIT 0x10000
46 #define SYNC_TYPE_MIN_BITS 0
47 #define SYNC_TYPE_MAX_BITS 3
48 #define ROOT_USER_UPPERBOUND 99
49 #define MAX_TIME_SIZE 1024
50 #define SYSTEM_USER_ID_MAX 99
51 #define BINARY_BASE 2
52 
CheckArraySize(const napi_env env,const AssetAttr & attr,uint32_t min,uint32_t max)53 bool CheckArraySize(const napi_env env, const AssetAttr &attr, uint32_t min, uint32_t max)
54 {
55     if (attr.value.blob.size > max || attr.value.blob.size <= min) {
56         NAPI_THROW_INVALID_ARGUMENT(env,
57             "Value byte length[%u] of tag[asset.Tag.%s] is out of range[%u, %u].",
58             attr.value.blob.size, TAG_MAP.at(attr.tag),  min + 1, max);
59         return false;
60     }
61     return true;
62 }
63 
CheckEnumVariant(const napi_env env,const AssetAttr & attr,const std::vector<uint32_t> & enumVec)64 bool CheckEnumVariant(const napi_env env, const AssetAttr &attr, const std::vector<uint32_t> &enumVec)
65 {
66     auto it = std::find(enumVec.begin(), enumVec.end(), attr.value.u32);
67     if (it == enumVec.end()) {
68         NAPI_THROW_INVALID_ARGUMENT(env,
69             "Value[%u] of tag[asset.Tag.%s] is an illegal enumeration variant.",
70             attr.value.u32, TAG_MAP.at(attr.tag));
71         return false;
72     }
73     return true;
74 }
75 
CheckNumberRange(const napi_env env,const AssetAttr & attr,uint32_t min,uint32_t max)76 bool CheckNumberRange(const napi_env env, const AssetAttr &attr, uint32_t min, uint32_t max)
77 {
78     if (attr.value.u32 > max || attr.value.u32 <= min) {
79         NAPI_THROW_INVALID_ARGUMENT(env,
80             "Value[%u] of tag[asset.Tag.%s] is out of range[%u, %u].",
81             attr.value.u32, TAG_MAP.at(attr.tag), min, max);
82         return false;
83     }
84     return true;
85 }
86 
CheckValidBits(const napi_env env,const AssetAttr & attr,uint32_t minBits,uint32_t maxBits)87 bool CheckValidBits(const napi_env env, const AssetAttr &attr, uint32_t minBits, uint32_t maxBits)
88 {
89     if (attr.value.u32 >= pow(BINARY_BASE, maxBits) || attr.value.u32 < pow(BINARY_BASE, minBits) - 1) {
90         NAPI_THROW_INVALID_ARGUMENT(env,
91             "Value[%u] of tag[asset.Tag.%s] has bit count out of range[%u, %u].",
92             attr.value.u32, TAG_MAP.at(attr.tag), minBits + 1, maxBits);
93         return false;
94     }
95     return true;
96 }
97 
CheckTagRange(const napi_env env,const AssetAttr & attr,const std::vector<uint32_t> & tags)98 bool CheckTagRange(const napi_env env, const AssetAttr &attr, const std::vector<uint32_t> &tags)
99 {
100     auto it = std::find(tags.begin(), tags.end(), attr.value.u32);
101     if (it == tags.end()) {
102         NAPI_THROW_INVALID_ARGUMENT(env,
103             "Value[0x%X] of tag[asset.Tag.(%s)] is not tags allowed for sorting, "
104             "which should start with \"DATA_LABEL\".", attr.value.u32, TAG_MAP.at(attr.tag));
105         return false;
106     }
107     return true;
108 }
109 
110 struct CheckContinuousRange {
111     std::function<bool(const napi_env, const AssetAttr &, uint32_t, uint32_t)> funcPtr;
112     uint32_t min;
113     uint32_t max;
114 };
115 
116 const std::unordered_map<uint32_t, CheckContinuousRange> CHECK_CONTINOUS_RANGE_FUNC_MAP = {
117     { SEC_ASSET_TAG_SECRET, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_SECRET_SIZE } },
118     { SEC_ASSET_TAG_ALIAS, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_ALIAS_SIZE } },
119     { SEC_ASSET_TAG_AUTH_VALIDITY_PERIOD, { &CheckNumberRange, MIN_NUMBER_VALUE, MAX_AUTH_VALID_PERIOD } },
120     { SEC_ASSET_TAG_AUTH_CHALLENGE, { &CheckArraySize, CHALLENGE_SIZE - 1, CHALLENGE_SIZE } },
121     { SEC_ASSET_TAG_AUTH_TOKEN, { &CheckArraySize, AUTH_TOKEN_SIZE - 1, AUTH_TOKEN_SIZE } },
122     { SEC_ASSET_TAG_SYNC_TYPE, { &CheckValidBits, SYNC_TYPE_MIN_BITS, SYNC_TYPE_MAX_BITS } },
123     { SEC_ASSET_TAG_DATA_LABEL_CRITICAL_1, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_LABEL_SIZE } },
124     { SEC_ASSET_TAG_DATA_LABEL_CRITICAL_2, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_LABEL_SIZE } },
125     { SEC_ASSET_TAG_DATA_LABEL_CRITICAL_3, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_LABEL_SIZE } },
126     { SEC_ASSET_TAG_DATA_LABEL_CRITICAL_4, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_LABEL_SIZE } },
127     { SEC_ASSET_TAG_DATA_LABEL_NORMAL_1, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_LABEL_SIZE } },
128     { SEC_ASSET_TAG_DATA_LABEL_NORMAL_2, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_LABEL_SIZE } },
129     { SEC_ASSET_TAG_DATA_LABEL_NORMAL_3, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_LABEL_SIZE } },
130     { SEC_ASSET_TAG_DATA_LABEL_NORMAL_4, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_LABEL_SIZE } },
131     { SEC_ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_1, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_LABEL_SIZE } },
132     { SEC_ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_2, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_LABEL_SIZE } },
133     { SEC_ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_3, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_LABEL_SIZE } },
134     { SEC_ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_4, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_LABEL_SIZE } },
135     { SEC_ASSET_TAG_RETURN_LIMIT, { &CheckNumberRange, MIN_NUMBER_VALUE, MAX_RETURN_LIMIT } },
136     { SEC_ASSET_TAG_USER_ID, { &CheckNumberRange, ROOT_USER_UPPERBOUND, INT32_MAX } },
137     { SEC_ASSET_TAG_UPDATE_TIME, { &CheckArraySize, MIN_ARRAY_SIZE, MAX_TIME_SIZE } }
138 };
139 
140 struct CheckDiscreteRange {
141     std::function<bool(const napi_env, const AssetAttr &, const std::vector<uint32_t> &)> funcPtr;
142     const std::vector<uint32_t> validRange;
143 };
144 
145 const std::unordered_map<uint32_t, CheckDiscreteRange> CHECK_DISCRETE_RANGE_FUNC_MAP = {
146     { SEC_ASSET_TAG_ACCESSIBILITY, { &CheckEnumVariant, ASSET_ACCESSIBILITY_VEC } },
147     { SEC_ASSET_TAG_AUTH_TYPE, { &CheckEnumVariant, ASSET_AUTH_TYPE_VEC } },
148     { SEC_ASSET_TAG_CONFLICT_RESOLUTION, { &CheckEnumVariant, ASSET_CONFLICT_RESOLUTION_VEC } },
149     { SEC_ASSET_TAG_RETURN_TYPE, { &CheckEnumVariant, ASSET_RETURN_TYPE_VEC } },
150     { SEC_ASSET_TAG_RETURN_ORDERED_BY, { &CheckTagRange, ASSET_RETURN_ORDER_BY_TAGS } }
151 };
152 
153 } // anonymous namespace
154 
CheckAssetRequiredTag(const napi_env env,const std::vector<AssetAttr> & attrs,const std::vector<uint32_t> & requiredTags)155 bool CheckAssetRequiredTag(const napi_env env, const std::vector<AssetAttr> &attrs,
156     const std::vector<uint32_t> &requiredTags)
157 {
158     for (uint32_t requiredTag : requiredTags) {
159         auto it = std::find_if(attrs.begin(), attrs.end(), [requiredTag](const AssetAttr &attr) {
160             return attr.tag == requiredTag;
161         });
162         if (it == attrs.end()) {
163             NAPI_THROW_INVALID_ARGUMENT(env, "Missing required tag[asset.Tag.%s].", TAG_MAP.at(requiredTag));
164             return false;
165         }
166     }
167     return true;
168 }
169 
CheckAssetTagValidity(const napi_env env,const std::vector<AssetAttr> & attrs,const std::vector<uint32_t> & validTags)170 bool CheckAssetTagValidity(const napi_env env, const std::vector<AssetAttr> &attrs,
171     const std::vector<uint32_t> &validTags)
172 {
173     for (AssetAttr attr : attrs) {
174         if (std::count(validTags.begin(), validTags.end(), attr.tag) == 0) {
175             NAPI_THROW_INVALID_ARGUMENT(env, "Unsupported tag[asset.Tag.%s] for the function.",
176                 TAG_MAP.at(attr.tag));
177             return false;
178         }
179     }
180     return true;
181 }
182 
CheckAssetValueValidity(const napi_env env,const std::vector<AssetAttr> & attrs)183 bool CheckAssetValueValidity(const napi_env env, const std::vector<AssetAttr> &attrs)
184 {
185     return std::all_of(attrs.begin(), attrs.end(), [env](const AssetAttr &attr) {
186         if (CHECK_CONTINOUS_RANGE_FUNC_MAP.find(attr.tag) != CHECK_CONTINOUS_RANGE_FUNC_MAP.end()) {
187             auto funcPtr = CHECK_CONTINOUS_RANGE_FUNC_MAP.at(attr.tag).funcPtr;
188             uint32_t min = CHECK_CONTINOUS_RANGE_FUNC_MAP.at(attr.tag).min;
189             uint32_t max = CHECK_CONTINOUS_RANGE_FUNC_MAP.at(attr.tag).max;
190             return funcPtr(env, attr, min, max);
191         }
192         if (CHECK_DISCRETE_RANGE_FUNC_MAP.find(attr.tag) != CHECK_DISCRETE_RANGE_FUNC_MAP.end()) {
193             auto funcPtr = CHECK_DISCRETE_RANGE_FUNC_MAP.at(attr.tag).funcPtr;
194             auto validRangePtr = CHECK_DISCRETE_RANGE_FUNC_MAP.at(attr.tag).validRange;
195             return funcPtr(env, attr, validRangePtr);
196         }
197         return true;
198         });
199 }
200 
201 } // Asset
202 } // Security
203 } // OHOS
204