• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = -1;
75     bool ret = Asn1Utils::BytesToInt(srcData_, tagStart, offset - tagStart, tag);
76     if (!ret || tag < 0) {
77         TELEPHONY_LOGE("Cannot parse tag at position: %{public}u", tagStart);
78         return nullptr;
79     }
80     return BuildAsn1Node(static_cast<uint32_t>(tag), offset, tagStart);
81 }
82 
BuildAsn1Node(const uint32_t tag,uint32_t offset,uint32_t tagStart)83 std::shared_ptr<Asn1Node> Asn1Decoder::BuildAsn1Node(const uint32_t tag, uint32_t offset, uint32_t tagStart)
84 {
85     if (srcData_.empty() || offset >= srcData_.size()) {
86         TELEPHONY_LOGE("srcData_ is empty");
87         return nullptr;
88     }
89     uint32_t dataLen;
90     uint8_t byteLen = srcData_[offset];
91     offset++;
92     // The highest bit being 1 indicates that the following 7 bits represent the length of the length field.
93     if ((byteLen & BIT8_MASK) == 0) {
94         dataLen = static_cast<uint32_t>(byteLen);
95     } else {
96         uint32_t lenLen = static_cast<uint32_t>(byteLen & MAX_INT8);
97         if (offset + lenLen > end_) {
98             TELEPHONY_LOGE("Cannot parse tag at position: %{public}u", tagStart);
99             return nullptr;
100         }
101         int32_t len = 0;
102         if (!Asn1Utils::BytesToInt(srcData_, offset, lenLen, len)) {
103             return nullptr;
104         }
105         dataLen = static_cast<uint32_t>(len);
106         offset += lenLen;
107     }
108     if (offset + dataLen > end_) {
109         TELEPHONY_LOGE("Incomplete data at position: offset=%{public}u, dataLen=%{public}u, leftLength=%{public}u.",
110             offset, dataLen, (end_ - offset));
111         return nullptr;
112     }
113     std::vector<uint8_t> byteStream(srcData_.begin() + offset, srcData_.begin() + offset + dataLen);
114     std::shared_ptr<Asn1Node> asn1Node = std::make_shared<Asn1Node>(tag, byteStream, 0, dataLen);
115     position_ = offset + dataLen;
116     return asn1Node;
117 }
118 } // namespace Telephony
119 }
120