1 /*
2  * Copyright (C) 2021 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 OHOS_USIM_ACCESS_RULE_H
17 #define OHOS_USIM_ACCESS_RULE_H
18 
19 #include <string>
20 #include <vector>
21 
22 namespace OHOS {
23 namespace Telephony {
24 class IccOperatorRule {
25 public:
26     inline constexpr static std::string_view TAG_ALL_RULE = "FF40";
27     inline constexpr static std::string_view TAG_RULE = "E2";
28     inline constexpr static std::string_view TAG_CERT_PKG = "E1";
29     inline constexpr static std::string_view TAG_CERTIFICATE = "C1";
30     inline constexpr static std::string_view TAG_PACKAGE = "CA";
31     inline constexpr static std::string_view TAG_LIMITS = "E3";
32     inline constexpr static std::string_view TAG_LIMIT = "DB";
33     inline constexpr static int32_t TLV_SIMPLE_TAG_ONEBYTE_LEN = 1;
34     inline constexpr static int32_t TLV_SIMPLE_TAG_LENGTH_LEN = 1;
35     inline constexpr static int32_t TLV_LEN_CHARLEN = 2;
36     inline constexpr static int32_t TLV_STAG_CHARLEN = 2;
37     inline constexpr static int32_t TLV_ONEBYTE_CHARLEN = 2;
38     inline constexpr static int32_t HEX_LEN = 10;
39     inline constexpr static int32_t HEX_ = 16;
40 
41 public:
42     static bool CreateFromTLV(const std::string::const_iterator &hexStrBeg,
43         const std::string::const_iterator &hexStrEnd, std::vector<IccOperatorRule> &result);
44     static bool CreateFromTLV(const std::string &hexStr, std::vector<IccOperatorRule> &result);
45 
46 public:
47     IccOperatorRule();
48     virtual ~IccOperatorRule();
49 
50     bool Matche(const std::string_view &certHash, const std::string_view &packageName) const;
51 
52     void GetPackageName(std::string &result) const;
53     void SetPackageName(const std::string &packageName);
54     void SetPackageName(std::string &&packageName);
55     bool SetPackageNameByHexStr(const std::string &hexStr);
56 
57     void GetCertificate(std::string &result) const;
58     void SetCertificate(const std::string &certificate);
59     void SetCertificate(std::string &&certificate);
60 
61     void SetAccessLimit(const std::string &accessLimit);
62     void SetAccessLimit(std::string &&accessLimit);
63 
64 private:
65     static bool DecodeTLVTagRule(std::string::const_iterator &hexStrBeg,
66         const std::string::const_iterator &hexStrEnd, IccOperatorRule &result, int32_t &len);
67     static bool DecodeTLVTagCertPkg(std::string::const_iterator &hexStrBeg,
68         const std::string::const_iterator &hexStrEnd, IccOperatorRule &result);
69     static bool DecodeTLVTagLimits(std::string::const_iterator &hexStrBeg,
70         const std::string::const_iterator &hexStrEnd, IccOperatorRule &result);
71 
72 private:
73     std::string packageName_;
74     std::string certificate_;
75     std::string accessLimit_;
76 };
77 } // namespace Telephony
78 } // namespace OHOS
79 
80 #endif
81