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 "asn1_builder.h"
17
18 #include <cctype>
19 #include <cstdio>
20 #include <securec.h>
21 #include "asn1_constants.h"
22 #include "asn1_node.h"
23 #include "asn1_utils.h"
24 #include "telephony_errors.h"
25
26 namespace OHOS {
27 namespace Telephony {
28 namespace {
29 const uint32_t FIVE_BYTE_LENGTH = 5;
30 const uint32_t ONE_BYTE_OCCPUPIED_BIT_COUNT = 8;
31 const uint32_t MAX_ENCODE_DATA_LENGTH = 10240;
32 }
33
Asn1AddChild(const std::shared_ptr<Asn1Node> node)34 void Asn1Builder::Asn1AddChild(const std::shared_ptr<Asn1Node> node)
35 {
36 std::lock_guard<std::mutex> lock(mutex_);
37 children_.push_back(node);
38 }
39
Asn1AddChildAsBytes(uint32_t tag,const std::vector<uint8_t> & childByte,uint32_t byteLen)40 int32_t Asn1Builder::Asn1AddChildAsBytes(uint32_t tag, const std::vector<uint8_t> &childByte, uint32_t byteLen)
41 {
42 TELEPHONY_LOGD("enter Asn1AddChildAsBytes");
43 if (Asn1Utils::IsConstructedTag(tag)) {
44 TELEPHONY_LOGE("Cannot set value of a constructed tag: %{public}u", tag);
45 return TELEPHONY_ERR_FAIL;
46 }
47 std::shared_ptr<Asn1Node> node = std::make_shared<Asn1Node>(tag, childByte, 0, byteLen);
48 Asn1AddChild(node);
49 return TELEPHONY_ERR_SUCCESS;
50 }
51
Asn1AddChildAsString(uint32_t tag,const std::string & childStr)52 int32_t Asn1Builder::Asn1AddChildAsString(uint32_t tag, const std::string &childStr)
53 {
54 TELEPHONY_LOGD("enter Asn1AddChildAsString");
55 std::vector<uint8_t> bytes = Asn1Utils::StringToBytes(childStr);
56 return Asn1AddChildAsBytes(tag, bytes, bytes.size());
57 }
58
Asn1AddChildAsInteger(uint32_t tag,uint32_t childInt)59 int32_t Asn1Builder::Asn1AddChildAsInteger(uint32_t tag, uint32_t childInt)
60 {
61 TELEPHONY_LOGD("enter Asn1AddChildAsInteger");
62 if (Asn1Utils::IsConstructedTag(tag)) {
63 TELEPHONY_LOGE("Cannot set value of a constructed tag: %{public}u", tag);
64 return TELEPHONY_ERR_FAIL;
65 }
66 std::vector<uint8_t> intBytes;
67 uint32_t bytesLen = Asn1Utils::UintToBytes(childInt, intBytes);
68 if (bytesLen == 0 || intBytes.empty()) {
69 TELEPHONY_LOGE("failed to transform uint data to bytes.");
70 return TELEPHONY_ERR_FAIL;
71 }
72
73 std::shared_ptr<Asn1Node> node = std::make_shared<Asn1Node>(tag, intBytes, 0, bytesLen);
74 Asn1AddChild(node);
75 return TELEPHONY_ERR_SUCCESS;
76 }
77
Asn1AddChildAsSignedInteger(uint32_t tag,int32_t childSignedInt)78 int32_t Asn1Builder::Asn1AddChildAsSignedInteger(uint32_t tag, int32_t childSignedInt)
79 {
80 TELEPHONY_LOGD("enter Asn1AddChildAsSignedInteger");
81 if (Asn1Utils::IsConstructedTag(tag)) {
82 TELEPHONY_LOGE("Cannot set value of a constructed tag: %{public}u", tag);
83 return TELEPHONY_ERR_FAIL;
84 }
85 std::vector<uint8_t> intBytes;
86 uint32_t bytesLen = Asn1Utils::IntToBytes(childSignedInt, intBytes);
87 std::shared_ptr<Asn1Node> node = std::make_shared<Asn1Node>(tag, intBytes, 0, bytesLen);
88 Asn1AddChild(node);
89 return TELEPHONY_ERR_SUCCESS;
90 }
91
Asn1AddChildAsBits(uint32_t tag,int32_t childBits)92 int32_t Asn1Builder::Asn1AddChildAsBits(uint32_t tag, int32_t childBits)
93 {
94 TELEPHONY_LOGD("enter Asn1AddChildAsBits");
95 if (Asn1Utils::IsConstructedTag(tag)) {
96 TELEPHONY_LOGE("Cannot set value of a constructed tag: %{public}u", tag);
97 return TELEPHONY_ERR_FAIL;
98 }
99
100 uint32_t dataLength = 0;
101 std::vector<uint8_t> childByte(FIVE_BYTE_LENGTH, '\0');
102 uint32_t reverseInt = Asn1Utils::ReverseInt(childBits);
103 for (uint32_t i = 1; i < childByte.size(); i++) {
104 childByte[i] = static_cast<uint8_t>(reverseInt >> ((sizeof(uint32_t) - i) * ONE_BYTE_OCCPUPIED_BIT_COUNT));
105 if (childByte[i] != 0) {
106 dataLength = i;
107 }
108 }
109 dataLength++;
110 childByte[0] = Asn1Utils::CountTrailingZeros(childByte[dataLength - 1]);
111 std::vector<uint8_t> newChildByte(childByte.begin(), childByte.begin() + dataLength);
112 std::shared_ptr<Asn1Node> node = std::make_shared<Asn1Node>(tag, newChildByte, 0, dataLength);
113 Asn1AddChild(node);
114 return TELEPHONY_ERR_SUCCESS;
115 }
116
Asn1AddChildAsBoolean(uint32_t tag,bool flag)117 int32_t Asn1Builder::Asn1AddChildAsBoolean(uint32_t tag, bool flag)
118 {
119 TELEPHONY_LOGD("enter Asn1AddChildAsBoolean");
120 std::vector<uint8_t> boolByteVec = {};
121 if (flag) {
122 boolByteVec.push_back(static_cast<uint8_t>(MAX_UINT8));
123 } else {
124 boolByteVec.push_back(static_cast<uint8_t>(0));
125 }
126 int32_t ret = Asn1AddChildAsBytes(tag, boolByteVec, boolByteVec.size());
127 return ret;
128 }
129
Asn1Build()130 std::shared_ptr<Asn1Node> Asn1Builder::Asn1Build()
131 {
132 TELEPHONY_LOGD("enter Asn1Build");
133 std::vector<uint8_t> byteStream = {};
134 std::shared_ptr<Asn1Node> newNode = std::make_shared<Asn1Node>(tag_, byteStream, 0, 0);
135 if (newNode == nullptr) {
136 TELEPHONY_LOGE("newNode is nullptr.");
137 return nullptr;
138 }
139 // calculate newNode's length, and move asn1Node from builder to newNode
140 std::shared_ptr<Asn1Node> asn1Node = nullptr;
141 uint32_t dataLen = 0;
142 std::lock_guard<std::mutex> lock(mutex_);
143 if (children_.size() > MAX_UINT8) {
144 TELEPHONY_LOGE("children_ is out of the bounds.");
145 return nullptr;
146 }
147
148 for (auto it = children_.begin(); it != children_.end(); ++it) {
149 asn1Node = *it;
150 if (asn1Node == nullptr) {
151 break;
152 }
153 if (asn1Node->GetEncodedLength() > MAX_ENCODE_DATA_LENGTH) {
154 return nullptr;
155 }
156 dataLen += asn1Node->GetEncodedLength();
157 newNode->AddNodeChildren(std::move(asn1Node));
158 }
159 children_.clear();
160 newNode->SetDataLength(dataLen);
161
162 // refresh node info
163 newNode->SetConstructed(true);
164 newNode->SetEncodedLength(Asn1Utils::ByteCountForUint(tag_) +
165 Asn1Utils::CalculateEncodedBytesNumForLength(dataLen) + dataLen);
166 return newNode;
167 }
168
Asn1BuilderToHexStr(std::string & destStr)169 uint32_t Asn1Builder::Asn1BuilderToHexStr(std::string &destStr)
170 {
171 TELEPHONY_LOGD("enter Asn1BuilderToHexStr");
172 uint32_t strLen = 0;
173 std::shared_ptr<Asn1Node> node = Asn1Build();
174 if (node == nullptr) {
175 return strLen;
176 }
177 strLen = node->Asn1NodeToHexStr(destStr);
178 return strLen;
179 }
180 } // namespace Telephony
181 }