• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef AUTH_ATTRIBUTES_H
17 #define AUTH_ATTRIBUTES_H
18 
19 #include <vector>
20 #include <cstdint>
21 #include <iostream>
22 #include <map>
23 #include <algorithm>
24 #include "coauth_info_define.h"
25 #include "iremote_object.h"
26 #include "parcel.h"
27 #include "iremote_broker.h"
28 #include "coauth_hilog_wrapper.h"
29 
30 namespace OHOS {
31 namespace UserIAM {
32 namespace AuthResPool {
33 class AuthAttributes {
34 public:
35     AuthAttributes();
~AuthAttributes()36     ~AuthAttributes() {};
37     void clear();
38     int32_t GetBoolValue(AuthAttributeType attrType, bool &value);
39     int32_t GetUint32Value(AuthAttributeType attrType, uint32_t &value);
40     int32_t GetUint64Value(AuthAttributeType attrType, uint64_t &value);
41     int32_t GetUint64ArrayValue(AuthAttributeType attrType, std::vector<uint64_t> &value);
42     int32_t GetUint32ArrayValue(AuthAttributeType attrType, std::vector<uint32_t> &value);
43     int32_t GetUint8ArrayValue(AuthAttributeType attrType, std::vector<uint8_t> &value);
44     int32_t Pack(std::vector<uint8_t> &buffer);
45 
46     int32_t SetBoolValue(AuthAttributeType attrType, bool value);
47     int32_t SetUint32Value(AuthAttributeType attrType, uint32_t value);
48     int32_t SetUint64Value(AuthAttributeType attrType, uint64_t value);
49     int32_t SetUint32ArrayValue(AuthAttributeType attrType, std::vector<uint32_t> &value);
50     int32_t SetUint64ArrayValue(AuthAttributeType attrType, std::vector<uint64_t> &value);
51     int32_t SetUint8ArrayValue(AuthAttributeType attrType, std::vector<uint8_t> &value);
52     AuthAttributes* Unpack(std::vector<uint8_t> &buffer);
53     enum ValueType {
54         BOOLTYPE = 1,
55         UINT32TYPE = 2,
56         UINT64TYPE = 3,
57         UINT32ARRAYTYPE = 4,
58         UINT8ARRAYTYPE = 5,
59         UINT64ARRAYTYPE = 6
60     };
61     DECLARE_INTERFACE_DESCRIPTOR(u"ohos.UserIAM.AuthResPool.AuthAttributes");
62 
63 private:
64     std::map<AuthAttributeType, bool> boolValueMap_;
65     std::map<AuthAttributeType, uint32_t> uint32ValueMap_;
66     std::map<AuthAttributeType, uint64_t> uint64ValueMap_;
67     std::map<AuthAttributeType, std::vector<uint32_t>> uint32ArraylValueMap_;
68     std::map<AuthAttributeType, std::vector<uint64_t>> uint64ArraylValueMap_;
69     std::map<AuthAttributeType, std::vector<uint8_t>> uint8ArrayValueMap_;
70     std::map<AuthAttributeType, ValueType> authAttributesPosition_;
71     std::vector<AuthAttributeType> existAttributes_;
72     AuthAttributeType GetUint32FromUint8(std::vector<uint8_t> &data, uint32_t begin);
73     bool GetBoolFromUint8(std::vector<uint8_t> &data, uint32_t begin);
74     uint64_t  GetUint64FromUint8(std::vector<uint8_t> &data, uint32_t begin);
75     std::vector<uint64_t> GetUint64ArrayFromUint8(std::vector<uint8_t> &data, uint32_t begin, uint32_t len);
76     std::vector<uint32_t> GetUint32ArrayFromUint8(std::vector<uint8_t> &data, uint32_t begin, uint32_t len);
77     void PackToBuffer(std::map<AuthAttributeType, ValueType>::iterator iter,
78                       uint32_t dataLength, uint8_t *writePointer,
79                       std::vector<uint8_t> &buffer);
80     void WriteDataLength(std::vector<uint8_t> &buffer, uint8_t *writePointer, uint32_t dataLength);
81     void UnpackTag(AuthAttributeType &tag, std::vector<uint8_t> &buffer,
82                    uint32_t &authDataLength, uint32_t &dataLength);
83     void Write32Array(std::vector<uint32_t> &uint32ArraylValue, uint8_t *writePointer,
84                       std::vector<uint8_t> &buffer);
85     void Write64Array(std::vector<uint64_t> &uint64ArraylValue, uint8_t *writePointer,
86                       std::vector<uint8_t> &buffer);
87     bool CheckLengthPass(ValueType type, uint32_t currIndex, uint32_t dataLength, uint32_t bufferLength);
88     void UnpackUint32ArrayType(std::vector<uint8_t> &buffer, AuthAttributeType tag, uint32_t &authDataLength,
89         uint32_t &dataLength);
90     void UnpackUint64ArrayType(std::vector<uint8_t> &buffer, AuthAttributeType tag, uint32_t &authDataLength,
91         uint32_t &dataLength);
92     void UnpackUint8ArrayType(std::vector<uint8_t> &buffer, AuthAttributeType tag, uint32_t &authDataLength,
93         uint32_t &dataLength);
94 };
95 } // namespace AuthResPool
96 } // namespace UserIAM
97 } // namespace OHOS
98 
99 #endif  // AUTH_ATTRIBUTES_H