1 /*
2  * Copyright (c) 2023 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 SYS_INSTALLER_MODULE_FILE_H
17 #define SYS_INSTALLER_MODULE_FILE_H
18 
19 #include <list>
20 #include <memory>
21 #include <optional>
22 #include <string>
23 
24 #ifdef SUPPORT_HVB
25 #include "hvb.h"
26 #endif
27 #include "module_zip_helper.h"
28 namespace OHOS {
29 namespace SysInstaller {
30 namespace {
31 constexpr size_t FS_TYPE_MAX_SIZE = 10;
32 }
33 
34 constexpr const char *HMP_APP_TYPE = "APP";
35 constexpr const char *HMP_SA_TYPE = "systemAbility";
36 constexpr const char *HMP_SA_TYPE_OLD = "SYSTEM ability";
37 constexpr const char *HMP_MIX_TYPE = "combineType";
38 constexpr const char *HMP_API_VERSION = "apiVersion";
39 constexpr const char *HMP_SA_SDK_VERSION = "saSdkVersion";
40 
41 bool ExtractZipFile(ModuleZipHelper &helper, const std::string &fileName, std::string &buf);
42 bool ParseVersion(const std::string &version, const std::string &split, std::vector<std::string> &versionVec);
43 bool CompareHmpVersion(const std::vector<std::string> &smallVersion, const std::vector<std::string> &bigVersion);
44 bool CompareSaSdkVersion(const std::vector<std::string> &smallVersion, const std::vector<std::string> &bigVersion);
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 int32_t VerifyModulePackageSign(const std::string &path);
49 bool EnhancedVerifyModule(const uint8_t *addr, uint64_t size);
50 #ifdef __cplusplus
51 }
52 #endif
53 
54 struct SaVersion {
55     uint32_t apiVersion;
56     uint32_t versionCode;
57     uint32_t patchVersion;
58 
stringSaVersion59     operator std::string() const
60     {
61         return std::to_string(apiVersion) + std::to_string(versionCode) + std::to_string(patchVersion);
62     }
63 };
64 
65 struct SaInfo {
66     std::string saName;
67     int32_t saId {0};
68     SaVersion version;
69 };
70 
71 struct BundleInfo {
72     std::string bundleName;
73     std::string bundleVersion;
74 };
75 
76 struct ModulePackageInfo {
77     std::string hmpName;
78     std::string version;
79     std::string saSdkVersion;
80     std::string type;
81     int apiVersion {-1};
82     int hotApply {0};
83     std::list<SaInfo> saInfoList;
84     std::list<BundleInfo> bundleInfoList;
85 };
86 
87 struct ImageStat {
88     uint32_t imageOffset;
89     uint32_t imageSize;
90     char fsType[FS_TYPE_MAX_SIZE];
91 };
92 
93 enum HmpInstallType {
94     COLD_SA_TYPE = 0x01,
95     COLD_APP_TYPE,
96     COLD_MIX_TYPE,
97     HOT_SA_TYPE = 0x10,
98     HOT_APP_TYPE,
99     HOT_MIX_TYPE
100 };
101 
102 class ModuleFile {
103 public:
104     static std::unique_ptr<ModuleFile> Open(const std::string &path);
105     static bool CompareVersion(const ModuleFile &newFile, const ModuleFile &oldFile);
ModuleFile(const std::string & modulePath,const ModulePackageInfo & versionInfo,const std::optional<ImageStat> & imageStat)106     ModuleFile(const std::string &modulePath,
107                const ModulePackageInfo &versionInfo,
108                const std::optional<ImageStat> &imageStat)
109         : modulePath_(modulePath),
110           versionInfo_(versionInfo),
111           imageStat_(imageStat) {}
112     virtual ~ModuleFile() = default;
113     ModuleFile(const ModuleFile&) = default;
114     ModuleFile& operator=(const ModuleFile&) = default;
115     ModuleFile(ModuleFile&&) = default;
116     ModuleFile& operator=(ModuleFile&&) = default;
117 
GetPath()118     const std::string &GetPath() const
119     {
120         return modulePath_;
121     }
GetVersionInfo()122     const ModulePackageInfo &GetVersionInfo() const
123     {
124         return versionInfo_;
125     }
GetImageStat()126     const std::optional<ImageStat> &GetImageStat() const
127     {
128         return imageStat_;
129     }
SetPath(const std::string & path)130     void SetPath(const std::string &path)
131     {
132         modulePath_ = path;
133     }
134     bool VerifyModuleVerity();
135     void ClearVerifiedData();
136     HmpInstallType GetHmpPackageType(void) const;
137 #ifdef SUPPORT_HVB
GetVerifiedData()138     struct hvb_verified_data *GetVerifiedData() const
139     {
140         return vd_;
141     }
142 #endif
143 
144 private:
145     std::string modulePath_;
146     ModulePackageInfo versionInfo_;
147     std::optional<ImageStat> imageStat_;
148 
149 #ifdef SUPPORT_HVB
150     struct hvb_verified_data *vd_ = nullptr;
151 #endif
152 };
153 } // namespace SysInstaller
154 } // namespace OHOS
155 #endif // SYS_INSTALLER_MODULE_FILE_H