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_decoder.h"
17 
18 #include <cctype>
19 #include <cstdio>
20 #include <securec.h>
21 #include "asn1_constants.h"
22 #include "asn1_utils.h"
23 
24 namespace OHOS {
25 namespace Telephony {
Asn1Decoder(const std::vector<uint8_t> &src, uint32_t offset, uint32_t decodeLen)26 Asn1Decoder::Asn1Decoder(const std::vector<uint8_t> &src, uint32_t offset, uint32_t decodeLen)
27 {
28     TELEPHONY_LOGD("enter Asn1Decoder");
29     if ((offset > (std::numeric_limits<uint32_t>::max() - decodeLen)) || ((offset + decodeLen) > src.size())) {
30         TELEPHONY_LOGE("Out of the bounds: byteLen=%{public}zu, offset=%{public}u, decodeLen=%{public}u.",
31             src.size(), offset, decodeLen);
32         return;
33     }
34 
35     srcData_ = src;
36     position_ = offset;
37     end_ = offset + decodeLen;
38 
39     TELEPHONY_LOGD("byteLen:%{public}zu, offset:%{public}u, decodeLen:%{public}u", src.size(), offset, decodeLen);
40 }
41 
Asn1HasNextNode()42 bool Asn1Decoder::Asn1HasNextNode()
43 {
44     return position_ < end_;
45 }
46 
Asn1NextNode()47 std::shared_ptr<Asn1Node> Asn1Decoder::Asn1NextNode()
48 {
49     TELEPHONY_LOGD("enter Asn1NextNode");
50     if (end_ <= position_ || srcData_.empty()) {
51         TELEPHONY_LOGE("No bytes to parse.");
52         return nullptr;
53     }
54 
55     uint32_t offset = position_;
56     uint32_t tagStart = offset;
57     if (offset >= srcData_.size()) {
58         return nullptr;
59     }
60     uint8_t byteTag = srcData_[offset];
61     offset++;
62     uint8_t lowFiveBit = 0x1F;
63     // the lower 5 bits of variable bitTag is tag number, 0x1F represent invalid val.
64     if ((byteTag & lowFiveBit) == lowFiveBit) {
65         while (offset < end_ && (static_cast<uint8_t>(srcData_[offset]) & BIT8_MASK) != 0) {
66             offset++;
67         }
68         offset++;
69     }
70     if (offset >= end_) {
71         TELEPHONY_LOGE("No bytes to parse.");
72         return nullptr;
73     }
74     int32_t tag = Asn1Utils::BytesToInt(srcData_, tagStart, offset - tagStart);
75     if (tag < 0) {
76         TELEPHONY_LOGE("Cannot parse tag at position: %{public}u", tagStart);
77         return nullptr;
78     }
79     return BuildAsn1Node(static_cast<uint32_t>(tag), offset, tagStart);
80 }
81 
BuildAsn1Node(const uint32_t tag, uint32_t offset, uint32_t tagStart)82 std::shared_ptr<Asn1Node> Asn1Decoder::BuildAsn1Node(const uint32_t tag, uint32_t offset, uint32_t tagStart)
83 {
84     if (srcData_.empty() || offset >= srcData_.size()) {
85         TELEPHONY_LOGE("srcData_ is empty");
86         return nullptr;
87     }
88     uint32_t dataLen;
89     uint8_t byteLen = srcData_[offset];
90     offset++;
91     // The highest bit being 1 indicates that the following 7 bits represent the length of the length field.
92     if ((byteLen & BIT8_MASK) == 0) {
93         dataLen = static_cast<uint32_t>(byteLen);
94     } else {
95         uint32_t lenLen = static_cast<uint32_t>(byteLen & MAX_INT8);
96         if (offset + lenLen > end_) {
97             TELEPHONY_LOGE("Cannot parse tag at position: %{public}u", tagStart);
98             return nullptr;
99         }
100         dataLen = static_cast<uint32_t>(Asn1Utils::BytesToInt(srcData_, offset, lenLen));
101         offset += lenLen;
102     }
103     if (offset + dataLen > end_) {
104         TELEPHONY_LOGE("Incomplete data at position: offset=%{public}u, dataLen=%{public}u, leftLength=%{public}u.",
105             offset, dataLen, (end_ - offset));
106         return nullptr;
107     }
108     std::vector<uint8_t> byteStream(srcData_.begin() + offset, srcData_.begin() + offset + dataLen);
109     std::shared_ptr<Asn1Node> asn1Node = std::make_shared<Asn1Node>(tag, byteStream, 0, dataLen);
110     position_ = offset + dataLen;
111     return asn1Node;
112 }
113 } // namespace Telephony
114 }
115