• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 <fstream>
17 #include <algorithm>
18 #include <stdexcept>
19 
20 #include "profile_verify.h"
21 #include "hap_utils.h"
22 
23 namespace OHOS {
24 namespace SignatureTools {
25 
26 const std::vector<int8_t> HapUtils::HAP_SIGNING_BLOCK_MAGIC_V2 =
27     std::vector<int8_t>{ 0x48, 0x41, 0x50, 0x20, 0x53, 0x69, 0x67, 0x20, 0x42,
28     0x6c, 0x6f, 0x63, 0x6b, 0x20, 0x34, 0x32 };
29 const std::vector<int8_t> HapUtils::HAP_SIGNING_BLOCK_MAGIC_V3 =
30     std::vector<int8_t>{ 0x3c, 0x68, 0x61, 0x70, 0x20, 0x73, 0x69, 0x67, 0x6e,
31     0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x3e };
32 const std::string HapUtils::HEX_CHAR_ARRAY = "0123456789ABCDEF";
33 const std::string HapUtils::HAP_DEBUG_OWNER_ID = "DEBUG_LIB_ID";
34 std::set<int> HapUtils::HAP_SIGNATURE_OPTIONAL_BLOCK_IDS;
35 
StaticConstructor()36 HapUtils::StaticConstructor::StaticConstructor()
37 {
38     HAP_SIGNATURE_OPTIONAL_BLOCK_IDS.insert(HAP_PROOF_OF_ROTATION_BLOCK_ID);
39     HAP_SIGNATURE_OPTIONAL_BLOCK_IDS.insert(HAP_PROFILE_BLOCK_ID);
40     HAP_SIGNATURE_OPTIONAL_BLOCK_IDS.insert(HAP_PROPERTY_BLOCK_ID);
41 }
42 
43 HapUtils::StaticConstructor HapUtils::staticConstructor;
44 
GetAppIdentifier(const std::string & profileContent)45 std::string HapUtils::GetAppIdentifier(const std::string& profileContent)
46 {
47     std::pair<std::string, std::string> resultPair = ParseAppIdentifier(profileContent);
48 
49     std::string ownerID = resultPair.first;
50     std::string profileType = resultPair.second;
51 
52     if (profileType == "debug") {
53         return HAP_DEBUG_OWNER_ID;
54     } else if (profileType == "release") {
55         return ownerID;
56     } else {
57         return "";
58     }
59 }
60 
ParseAppIdentifier(const std::string & profileContent)61 std::pair<std::string, std::string> HapUtils::ParseAppIdentifier(const std::string& profileContent)
62 {
63     std::string ownerID;
64     std::string profileType;
65 
66     ProfileInfo provisionInfo;
67     ParseProfile(profileContent, provisionInfo);
68 
69     if (DEBUG == provisionInfo.type) {
70         profileType = "debug";
71     } else {
72         profileType = "release";
73     }
74 
75     BundleInfo bundleInfo = provisionInfo.bundleInfo;
76 
77     if (!bundleInfo.appIdentifier.empty()) {
78         ownerID = bundleInfo.appIdentifier;
79     }
80 
81     return std::pair(ownerID, profileType);
82 }
83 
GetHapSigningBlockMagic(int compatibleVersion)84 std::vector<int8_t> HapUtils::GetHapSigningBlockMagic(int compatibleVersion)
85 {
86     if (compatibleVersion >= MIN_COMPATIBLE_VERSION_FOR_SCHEMA_V3) {
87         return HAP_SIGNING_BLOCK_MAGIC_V3;
88     }
89     return HAP_SIGNING_BLOCK_MAGIC_V2;
90 }
91 
GetHapSigningBlockVersion(int compatibleVersion)92 int HapUtils::GetHapSigningBlockVersion(int compatibleVersion)
93 {
94     if (compatibleVersion >= MIN_COMPATIBLE_VERSION_FOR_SCHEMA_V3) {
95         return HAP_SIGN_SCHEME_V3_BLOCK_VERSION;
96     }
97     return HAP_SIGN_SCHEME_V2_BLOCK_VERSION;
98 }
99 
ReadFileToByteBuffer(const std::string & file,ByteBuffer & buffer)100 bool HapUtils::ReadFileToByteBuffer(const std::string& file, ByteBuffer& buffer)
101 {
102     std::string ret;
103     if (FileUtils::ReadFile(file, ret) < 0) {
104         PrintErrorNumberMsg("IO_ERROR", IO_ERROR, file + " not exist or can not read!");
105         return false;
106     }
107     buffer.SetCapacity(static_cast<int32_t>(ret.size()));
108     buffer.PutData(ret.data(), ret.size());
109     return true;
110 }
111 
112 } // namespace SignatureTools
113 } // namespace OHOS