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 #ifndef ASN1_NODE_H
17 #define ASN1_NODE_H
18 
19 #include <cstdbool>
20 #include <cstdint>
21 #include <list>
22 #include <mutex>
23 #include "telephony_log_wrapper.h"
24 
25 namespace OHOS {
26 namespace Telephony {
27 class Asn1Node {
28 public:
29     Asn1Node(const uint32_t tag, const std::vector<uint8_t> &src, uint32_t offset, uint32_t length);
30     uint32_t Asn1NodeToHexStr(std::string &destStr);
31     uint32_t Asn1NodeToBytes(std::vector<uint8_t> &dest);
32     std::shared_ptr<Asn1Node> Asn1GetChild(const uint32_t tag);
33     bool Asn1HasChild(const uint32_t tag);
34     std::shared_ptr<Asn1Node> Asn1GetGrandson(const uint32_t firstLevelTag, const uint32_t secondLevelTag);
35     std::shared_ptr<Asn1Node> Asn1GetGreatGrandson(const uint32_t firstLevelTag, const uint32_t secondLevelTag,
36         const uint32_t thirdLevelTag);
37     int32_t Asn1GetChildren(const uint32_t tag, std::list<std::shared_ptr<Asn1Node>> &children);
38     uint32_t Asn1GetHeadAsHexStr(std::string &headHex);
39     uint32_t Asn1AsBytes(std::vector<uint8_t> &output);
40     int32_t Asn1AsInteger();
41     uint32_t Asn1AsString(std::string &output);
42     int32_t Asn1AsBits();
43 
SetDataLength(const uint32_t dataLength)44     void SetDataLength(const uint32_t dataLength)
45     {
46         dataLength_ = dataLength;
47     }
48 
SetConstructed(bool constructed)49     void SetConstructed(bool constructed)
50     {
51         constructed_ = constructed;
52     }
53 
SetEncodedLength(const uint32_t encodedLength)54     void SetEncodedLength(const uint32_t encodedLength)
55     {
56         encodedLength_ = encodedLength;
57     }
58 
GetEncodedLength()59     uint32_t GetEncodedLength()
60     {
61         return encodedLength_ ;
62     }
63 
AddNodeChildren(const std::shared_ptr<Asn1Node> &asn1Node)64     void AddNodeChildren(const std::shared_ptr<Asn1Node> &asn1Node)
65     {
66         std::lock_guard<std::mutex> lock(mutex_);
67         children_.push_back(asn1Node);
68     }
69 
GetNodeTag()70     uint32_t GetNodeTag()
71     {
72         return tag_;
73     }
74 
75 private:
76     int32_t Asn1BuildChildren();
77     void Asn1Write(std::vector<uint8_t> &dest);
78 
79 private:
80     uint32_t tag_ = 0;
81     std::list<std::shared_ptr<Asn1Node>> children_;
82     bool constructed_ = false;
83     std::vector<uint8_t> dataBytes_ = {};
84     uint32_t dataOffset_ = 0;
85     uint32_t dataLength_ = 0;
86     uint32_t encodedLength_ = 0;
87     std::mutex mutex_;
88 };
89 } // namespace Telephony
90 } // namespace OHOS
91 #endif // ASN1_NODE_H_