1 /*
2 * Copyright (c) 2022 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 "auth_attributes.h"
17 #include <cinttypes>
18
19 namespace OHOS {
20 namespace UserIAM {
21 namespace AuthResPool {
AuthAttributes()22 AuthAttributes::AuthAttributes()
23 {
24 authAttributesPosition_ = {{AUTH_RESULT_CODE, UINT32TYPE},
25 {AUTH_RESULT_CODE, UINT32TYPE},
26 {AUTH_SIGNATURE, UINT8ARRAYTYPE},
27 {AUTH_IDENTIFY_MODE, UINT32TYPE},
28 {AUTH_TEMPLATE_ID, UINT64TYPE},
29 {AUTH_TEMPLATE_ID_LIST, UINT64ARRAYTYPE},
30 {AUTH_REMAIN_COUNT, UINT32TYPE},
31 {AUTH_REMAIN_TIME, UINT32TYPE},
32 {AUTH_SCHEDULE_ID, UINT64TYPE},
33 {AUTH_CALLER_NAME, UINT8ARRAYTYPE},
34 {AUTH_SCHEDULE_VERSION, UINT32TYPE},
35 {AUTH_LOCK_OUT_TEMPLATE, UINT64TYPE},
36 {AUTH_UNLOCK_TEMPLATE, UINT64TYPE},
37 {AUTH_SUBTYPE, UINT64TYPE},
38 {AUTH_SCHEDULE_MODE, UINT32TYPE},
39 {AUTH_PROPERTY_MODE, UINT32TYPE},
40 {AUTH_TYPE, UINT32TYPE},
41 {AUTH_CREDENTIAL_ID, UINT64TYPE},
42 {AUTH_CONTROLLER, BOOLTYPE},
43 {AUTH_CALLER_UID, UINT64TYPE},
44 {AUTH_RESULT, UINT8ARRAYTYPE},
45 {ALGORITHM_INFO, UINT8ARRAYTYPE}};
46 }
47
clear()48 void AuthAttributes::clear()
49 {
50 boolValueMap_.clear();
51 uint32ValueMap_.clear();
52 uint64ValueMap_.clear();
53 uint32ArraylValueMap_.clear();
54 uint64ArraylValueMap_.clear();
55 uint8ArrayValueMap_.clear();
56 }
57
GetBoolValue(AuthAttributeType attrType,bool & value)58 int32_t AuthAttributes::GetBoolValue(AuthAttributeType attrType, bool &value)
59 {
60 std::map<AuthAttributeType, bool>::iterator iter = boolValueMap_.find(attrType);
61 if (iter == boolValueMap_.end()) {
62 return FAIL;
63 }
64 value = iter->second;
65 return SUCCESS;
66 }
67
GetUint32Value(AuthAttributeType attrType,uint32_t & value)68 int32_t AuthAttributes::GetUint32Value(AuthAttributeType attrType, uint32_t &value)
69 {
70 std::map<AuthAttributeType, uint32_t>::iterator iter = uint32ValueMap_.find(attrType);
71 if (iter == uint32ValueMap_.end()) {
72 return FAIL;
73 }
74 value = iter->second;
75 return SUCCESS;
76 }
77
GetUint64Value(AuthAttributeType attrType,uint64_t & value)78 int32_t AuthAttributes::GetUint64Value(AuthAttributeType attrType, uint64_t &value)
79 {
80 std::map<AuthAttributeType, uint64_t>::iterator iter = uint64ValueMap_.find(attrType);
81 if (iter == uint64ValueMap_.end()) {
82 return FAIL;
83 }
84 value = iter->second;
85 return SUCCESS;
86 }
87
GetUint32ArrayValue(AuthAttributeType attrType,std::vector<uint32_t> & value)88 int32_t AuthAttributes::GetUint32ArrayValue(AuthAttributeType attrType, std::vector<uint32_t> &value)
89 {
90 std::map<AuthAttributeType, std::vector<uint32_t>>::iterator iter = uint32ArraylValueMap_.find(attrType);
91 if (iter == uint32ArraylValueMap_.end()) {
92 return FAIL;
93 }
94 value = iter->second;
95 return SUCCESS;
96 }
97
GetUint64ArrayValue(AuthAttributeType attrType,std::vector<uint64_t> & value)98 int32_t AuthAttributes::GetUint64ArrayValue(AuthAttributeType attrType, std::vector<uint64_t> &value)
99 {
100 std::map<AuthAttributeType, std::vector<uint64_t>>::iterator iter = uint64ArraylValueMap_.find(attrType);
101 if (iter == uint64ArraylValueMap_.end()) {
102 return FAIL;
103 }
104 value = iter->second;
105 return SUCCESS;
106 }
107
GetUint8ArrayValue(AuthAttributeType attrType,std::vector<uint8_t> & value)108 int32_t AuthAttributes::GetUint8ArrayValue(AuthAttributeType attrType, std::vector<uint8_t> &value)
109 {
110 std::map<AuthAttributeType, std::vector<uint8_t>>::iterator iter = uint8ArrayValueMap_.find(attrType);
111 if (iter == uint8ArrayValueMap_.end()) {
112 return FAIL;
113 }
114 value = iter->second;
115 return SUCCESS;
116 }
117
SetBoolValue(AuthAttributeType attrType,bool value)118 int32_t AuthAttributes::SetBoolValue(AuthAttributeType attrType, bool value)
119 {
120 if (authAttributesPosition_[attrType] != BOOLTYPE) {
121 return FAIL;
122 }
123 boolValueMap_[attrType] = value;
124 existAttributes_.push_back(attrType);
125 return SUCCESS;
126 }
127
SetUint32Value(AuthAttributeType attrType,uint32_t value)128 int32_t AuthAttributes::SetUint32Value(AuthAttributeType attrType, uint32_t value)
129 {
130 if (authAttributesPosition_[attrType] != UINT32TYPE) {
131 return FAIL;
132 }
133 uint32ValueMap_[attrType] = value;
134 existAttributes_.push_back(attrType);
135 COAUTH_HILOGD(MODULE_INNERKIT, "SetUint32Value : %{public}u.", value);
136 return SUCCESS;
137 }
138
SetUint64Value(AuthAttributeType attrType,uint64_t value)139 int32_t AuthAttributes::SetUint64Value(AuthAttributeType attrType, uint64_t value)
140 {
141 if (authAttributesPosition_[attrType] != UINT64TYPE) {
142 return FAIL;
143 }
144 uint64ValueMap_[attrType] = value;
145 existAttributes_.push_back(attrType);
146 return SUCCESS;
147 }
148
SetUint32ArrayValue(AuthAttributeType attrType,std::vector<uint32_t> & value)149 int32_t AuthAttributes::SetUint32ArrayValue(AuthAttributeType attrType, std::vector<uint32_t> &value)
150 {
151 if (authAttributesPosition_[attrType] != UINT32ARRAYTYPE) {
152 return FAIL;
153 }
154 uint32ArraylValueMap_[attrType] = value;
155 existAttributes_.push_back(attrType);
156 return SUCCESS;
157 }
158
SetUint64ArrayValue(AuthAttributeType attrType,std::vector<uint64_t> & value)159 int32_t AuthAttributes::SetUint64ArrayValue(AuthAttributeType attrType, std::vector<uint64_t> &value)
160 {
161 if (authAttributesPosition_[attrType] != UINT64ARRAYTYPE) {
162 return FAIL;
163 }
164 uint64ArraylValueMap_[attrType] = value;
165 existAttributes_.push_back(attrType);
166 return SUCCESS;
167 }
168
SetUint8ArrayValue(AuthAttributeType attrType,std::vector<uint8_t> & value)169 int32_t AuthAttributes::SetUint8ArrayValue(AuthAttributeType attrType, std::vector<uint8_t> &value)
170 {
171 if (authAttributesPosition_[attrType] != UINT8ARRAYTYPE) {
172 return FAIL;
173 }
174 uint8ArrayValueMap_[attrType] = value;
175 existAttributes_.push_back(attrType);
176 return SUCCESS;
177 }
178
UnpackTag(AuthAttributeType & tag,std::vector<uint8_t> & buffer,uint32_t & authDataLength,uint32_t & dataLength)179 void AuthAttributes::UnpackTag(AuthAttributeType &tag, std::vector<uint8_t> &buffer,
180 uint32_t &authDataLength, uint32_t &dataLength)
181 {
182 tag = GetUint32FromUint8(buffer, authDataLength);
183 authDataLength += sizeof(uint32_t);
184 dataLength = static_cast<uint32_t>(GetUint32FromUint8(buffer, authDataLength));
185 authDataLength += sizeof(uint32_t);
186 }
187
CheckLengthPass(ValueType type,uint32_t currIndex,uint32_t dataLength,uint32_t bufferLength)188 bool AuthAttributes::CheckLengthPass(ValueType type, uint32_t currIndex, uint32_t dataLength, uint32_t bufferLength)
189 {
190 if (currIndex + dataLength > bufferLength) {
191 COAUTH_HILOGE(MODULE_INNERKIT, "buffer read exceed buffer size");
192 return false;
193 }
194
195 switch (type) {
196 case BOOLTYPE:
197 if (dataLength != sizeof(bool)) {
198 COAUTH_HILOGE(MODULE_INNERKIT, "data length mismatch(bool)");
199 return false;
200 }
201 break;
202 case UINT32TYPE:
203 if (dataLength != sizeof(uint32_t)) {
204 COAUTH_HILOGE(MODULE_INNERKIT, "data length mismatch(uint32_t)");
205 return false;
206 }
207 break;
208 case UINT64TYPE:
209 if (dataLength != sizeof(uint64_t)) {
210 COAUTH_HILOGE(MODULE_INNERKIT, "data length mismatch(uint64_t)");
211 return false;
212 }
213 break;
214 default:
215 break;
216 }
217 return true;
218 }
219
UnpackUint32ArrayType(std::vector<uint8_t> & buffer,AuthAttributeType tag,uint32_t & authDataLength,uint32_t & dataLength)220 void AuthAttributes::UnpackUint32ArrayType(std::vector<uint8_t> &buffer, AuthAttributeType tag,
221 uint32_t &authDataLength, uint32_t &dataLength)
222 {
223 std::vector<uint32_t> uint32ArraylValue = GetUint32ArrayFromUint8(buffer, authDataLength, dataLength);
224 SetUint32ArrayValue(tag, uint32ArraylValue);
225 authDataLength += dataLength;
226 }
227
UnpackUint64ArrayType(std::vector<uint8_t> & buffer,AuthAttributeType tag,uint32_t & authDataLength,uint32_t & dataLength)228 void AuthAttributes::UnpackUint64ArrayType(std::vector<uint8_t> &buffer, AuthAttributeType tag,
229 uint32_t &authDataLength, uint32_t &dataLength)
230 {
231 std::vector<uint64_t> uint64ArraylValue = GetUint64ArrayFromUint8(buffer, authDataLength, dataLength);
232 SetUint64ArrayValue(tag, uint64ArraylValue);
233 authDataLength += dataLength;
234 }
235
UnpackUint8ArrayType(std::vector<uint8_t> & buffer,AuthAttributeType tag,uint32_t & authDataLength,uint32_t & dataLength)236 void AuthAttributes::UnpackUint8ArrayType(std::vector<uint8_t> &buffer, AuthAttributeType tag, uint32_t &authDataLength,
237 uint32_t &dataLength)
238 {
239 if (dataLength == 0) {
240 return;
241 }
242 std::vector<uint8_t> uint8ArrayValue(buffer.begin() + authDataLength,
243 buffer.begin() + authDataLength + dataLength);
244 SetUint8ArrayValue(tag, uint8ArrayValue);
245 authDataLength += dataLength;
246 }
247
Unpack(std::vector<uint8_t> & buffer)248 AuthAttributes* AuthAttributes::Unpack(std::vector<uint8_t> &buffer)
249 {
250 if (buffer.empty()) {
251 return nullptr;
252 }
253 uint32_t dataLength;
254 uint32_t authDataLength = 0;
255 AuthAttributeType tag;
256 // skip unused tag
257 UnpackTag(tag, buffer, authDataLength, dataLength);
258 UnpackTag(tag, buffer, authDataLength, dataLength);
259 while (authDataLength < buffer.size()) {
260 UnpackTag(tag, buffer, authDataLength, dataLength);
261 std::map<AuthAttributeType, ValueType>::iterator iter = authAttributesPosition_.find(tag);
262 if (!CheckLengthPass(iter->second, authDataLength, dataLength, buffer.size())) {
263 return nullptr;
264 }
265 COAUTH_HILOGE(MODULE_INNERKIT, "buffer read %{public}d", tag);
266 switch (iter->second) {
267 case BOOLTYPE:
268 SetBoolValue(tag, GetBoolFromUint8(buffer, authDataLength));
269 authDataLength += sizeof(bool);
270 break;
271 case UINT32TYPE:
272 SetUint32Value(tag, static_cast<uint32_t>(GetUint32FromUint8(buffer, authDataLength)));
273 authDataLength += sizeof(uint32_t);
274 break;
275 case UINT64TYPE:
276 SetUint64Value(tag, GetUint64FromUint8(buffer, authDataLength));
277 authDataLength += sizeof(uint64_t);
278 break;
279 case UINT32ARRAYTYPE:
280 UnpackUint32ArrayType(buffer, tag, authDataLength, dataLength);
281 break;
282 case UINT64ARRAYTYPE:
283 UnpackUint64ArrayType(buffer, tag, authDataLength, dataLength);
284 break;
285 case UINT8ARRAYTYPE:
286 UnpackUint8ArrayType(buffer, tag, authDataLength, dataLength);
287 break;
288 default:
289 break;
290 }
291 }
292 return this;
293 }
294
GetUint32FromUint8(std::vector<uint8_t> & data,uint32_t begin)295 AuthAttributeType AuthAttributes::GetUint32FromUint8(std::vector<uint8_t> &data, uint32_t begin)
296 {
297 if (begin >= data.size() || data.size() - begin < sizeof(uint32_t)) {
298 return ALGORITHM_INFO;
299 }
300 uint8_t tmp[sizeof(uint32_t)];
301 for (uint32_t i = 0; i < sizeof(uint32_t); i++) {
302 tmp[i] = data[begin + i];
303 }
304 AuthAttributeType *re = static_cast<AuthAttributeType *>(static_cast<void *>(tmp));
305 return *re;
306 }
307
GetBoolFromUint8(std::vector<uint8_t> & data,uint32_t begin)308 bool AuthAttributes::GetBoolFromUint8(std::vector<uint8_t> &data, uint32_t begin)
309 {
310 if (begin >= data.size() || data.size() - begin < sizeof(bool)) {
311 return 0;
312 }
313 uint8_t tmp = data[begin];
314 bool *re = static_cast<bool *>(static_cast<void *>(&tmp));
315 return *re;
316 }
317
GetUint64FromUint8(std::vector<uint8_t> & data,uint32_t begin)318 uint64_t AuthAttributes::GetUint64FromUint8(std::vector<uint8_t> &data, uint32_t begin)
319 {
320 if (begin >= data.size() || data.size() - begin < sizeof(uint64_t)) {
321 return 0;
322 }
323 uint8_t tmp[sizeof(uint64_t)];
324 for (uint32_t i = 0; i < sizeof(uint64_t); i++) {
325 tmp[i] = data[begin + i];
326 }
327 uint64_t *re = static_cast<uint64_t *>(static_cast<void *>(tmp));
328 return *re;
329 }
330
GetUint32ArrayFromUint8(std::vector<uint8_t> & data,uint32_t begin,uint32_t len)331 std::vector<uint32_t> AuthAttributes::GetUint32ArrayFromUint8(std::vector<uint8_t> &data, uint32_t begin, uint32_t len)
332 {
333 std::vector<uint32_t> tmp;
334 for (uint32_t i = 0; i < len / sizeof(uint32_t); i++) {
335 uint32_t uint32data = GetUint32FromUint8(data, begin + i * sizeof(uint32_t));
336 tmp.push_back(uint32data);
337 COAUTH_HILOGD(MODULE_INNERKIT, "buffer read uint32ArrayValue : %{public}u.", uint32data);
338 }
339 return tmp;
340 }
341
GetUint64ArrayFromUint8(std::vector<uint8_t> & data,uint32_t begin,uint32_t len)342 std::vector<uint64_t> AuthAttributes::GetUint64ArrayFromUint8(std::vector<uint8_t> &data, uint32_t begin, uint32_t len)
343 {
344 std::vector<uint64_t> tmp;
345 for (uint32_t i = 0; i < len / sizeof(uint64_t); i++) {
346 uint64_t uint64data = GetUint64FromUint8(data, begin + i * sizeof(uint64_t));
347 tmp.push_back(uint64data);
348 COAUTH_HILOGD(MODULE_INNERKIT, "buffer read uint64ArrayValue : %{public}" PRIu64, uint64data);
349 }
350 return tmp;
351 }
352
Pack(std::vector<uint8_t> & buffer)353 int32_t AuthAttributes::Pack(std::vector<uint8_t> &buffer)
354 {
355 uint8_t *writePointer;
356 uint32_t dataLength = 0;
357 uint32_t tag;
358 uint32_t authDataLength = 0;
359 buffer.clear();
360 sort(existAttributes_.begin(), existAttributes_.end());
361 for (uint32_t i = 0; i != existAttributes_.size(); i++) {
362 if (existAttributes_[i] == AUTH_ROOT ||
363 existAttributes_[i] == AUTH_DATA ||
364 existAttributes_[i] == AUTH_SIGNATURE) {
365 continue;
366 }
367 tag = authAttributesPosition_.find(existAttributes_[i])->first;
368 writePointer = static_cast<uint8_t*>(static_cast<void *>(&tag));
369 buffer.insert(buffer.end(), writePointer, writePointer + sizeof(AuthAttributeType));
370 COAUTH_HILOGD(MODULE_INNERKIT, "data Write tag : %{public}u.", tag);
371 PackToBuffer(authAttributesPosition_.find(existAttributes_[i]), dataLength, writePointer, buffer);
372 }
373
374 authDataLength = buffer.size();
375 writePointer = static_cast<uint8_t*>(static_cast<void *>(&authDataLength));
376 buffer.insert(buffer.begin(), writePointer, writePointer + sizeof(uint32_t));
377 tag = AUTH_DATA;
378 writePointer = static_cast<uint8_t*>(static_cast<void *>(&tag));
379 buffer.insert(buffer.begin(), writePointer, writePointer + sizeof(AuthAttributeType));
380
381 tag = AUTH_SIGNATURE;
382 writePointer = static_cast<uint8_t*>(static_cast<void *>(&tag));
383 buffer.insert(buffer.end(), writePointer, writePointer + sizeof(AuthAttributeType));
384
385 PackToBuffer(authAttributesPosition_.find(AUTH_SIGNATURE), dataLength, writePointer, buffer);
386
387 authDataLength = buffer.size();
388 writePointer = static_cast<uint8_t*>(static_cast<void *>(&authDataLength));
389 buffer.insert(buffer.begin(), writePointer, writePointer + sizeof(uint32_t));
390 tag = AUTH_ROOT;
391 writePointer = static_cast<uint8_t*>(static_cast<void *>(&tag));
392 buffer.insert(buffer.begin(), writePointer, writePointer + sizeof(AuthAttributeType));
393 return SUCCESS;
394 }
395
396
Write32Array(std::vector<uint32_t> & uint32ArraylValue,uint8_t * writePointer,std::vector<uint8_t> & buffer)397 void AuthAttributes::Write32Array(std::vector<uint32_t> &uint32ArraylValue, uint8_t *writePointer,
398 std::vector<uint8_t> &buffer)
399 {
400 for (std::size_t num = 0; num < uint32ArraylValue.size(); num++) {
401 writePointer = static_cast<uint8_t*>(static_cast<void *>(&uint32ArraylValue[num]));
402 buffer.insert(buffer.end(), writePointer, writePointer + sizeof(uint32_t));
403 }
404 }
Write64Array(std::vector<uint64_t> & uint64ArraylValue,uint8_t * writePointer,std::vector<uint8_t> & buffer)405 void AuthAttributes::Write64Array(std::vector<uint64_t> &uint64ArraylValue, uint8_t *writePointer,
406 std::vector<uint8_t> &buffer)
407 {
408 for (std::size_t num = 0; num < uint64ArraylValue.size(); num++) {
409 writePointer = static_cast<uint8_t*>(static_cast<void *>(&uint64ArraylValue[num]));
410 buffer.insert(buffer.end(), writePointer, writePointer + sizeof(uint64_t));
411 }
412 }
413
PackToBuffer(std::map<AuthAttributeType,ValueType>::iterator iter,uint32_t dataLength,uint8_t * writePointer,std::vector<uint8_t> & buffer)414 void AuthAttributes::PackToBuffer(std::map<AuthAttributeType, ValueType>::iterator iter,
415 uint32_t dataLength, uint8_t *writePointer, std::vector<uint8_t> &buffer)
416 {
417 bool boolValue;
418 uint32_t uint32Value;
419 uint64_t uint64Value;
420 std::vector<uint32_t> uint32ArraylValue;
421 std::vector<uint64_t> uint64ArraylValue;
422 std::vector<uint8_t> uint8ArrayValue;
423 switch (iter->second) {
424 case BOOLTYPE:
425 GetBoolValue(iter->first, boolValue);
426 WriteDataLength(buffer, writePointer, sizeof(bool));
427 writePointer = static_cast<uint8_t*>(static_cast<void *>(&boolValue));
428 buffer.insert(buffer.end(), writePointer, writePointer + sizeof(bool));
429 break;
430 case UINT32TYPE:
431 GetUint32Value(iter->first, uint32Value);
432 WriteDataLength(buffer, writePointer, sizeof(uint32_t));
433 writePointer = static_cast<uint8_t*>(static_cast<void *>(&uint32Value));
434 buffer.insert(buffer.end(), writePointer, writePointer + sizeof(uint32_t));
435 break;
436 case UINT64TYPE:
437 GetUint64Value(iter->first, uint64Value);
438 WriteDataLength(buffer, writePointer, sizeof(uint64_t));
439 writePointer = static_cast<uint8_t*>(static_cast<void *>(&uint64Value));
440 buffer.insert(buffer.end(), writePointer, writePointer + sizeof(uint64_t));
441 break;
442 case UINT32ARRAYTYPE:
443 GetUint32ArrayValue(iter->first, uint32ArraylValue);
444 WriteDataLength(buffer, writePointer, sizeof(uint32_t) * uint32ArraylValue.size());
445 Write32Array(uint32ArraylValue, writePointer, buffer);
446 break;
447 case UINT64ARRAYTYPE:
448 GetUint64ArrayValue(iter->first, uint64ArraylValue);
449 WriteDataLength(buffer, writePointer, sizeof(uint64_t) * uint64ArraylValue.size());
450 Write64Array(uint64ArraylValue, writePointer, buffer);
451 break;
452 case UINT8ARRAYTYPE:
453 if (GetUint8ArrayValue(iter->first, uint8ArrayValue)) {
454 WriteDataLength(buffer, writePointer, 0);
455 break;
456 }
457 dataLength = sizeof(uint8_t) * uint8ArrayValue.size();
458 WriteDataLength(buffer, writePointer, dataLength);
459 buffer.insert(buffer.end(), uint8ArrayValue.begin(), uint8ArrayValue.begin() + dataLength);
460 break;
461 default:
462 break;
463 }
464 }
465
WriteDataLength(std::vector<uint8_t> & buffer,uint8_t * writePointer,uint32_t dataLength)466 void AuthAttributes::WriteDataLength(std::vector<uint8_t> &buffer, uint8_t *writePointer, uint32_t dataLength)
467 {
468 writePointer = static_cast<uint8_t*>(static_cast<void *>(&dataLength));
469 buffer.insert(buffer.end(), writePointer, writePointer + sizeof(uint32_t));
470 }
471 } // AuthResPool
472 } // UserIAM
473 } // OHOS