• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
20 #include <optional>
21 #include <string>
22 
23 #ifdef SUPPORT_HVB
24 #include "hvb.h"
25 #endif
26 #include "module_zip_helper.h"
27 namespace OHOS {
28 namespace SysInstaller {
29 namespace {
30 constexpr size_t FS_TYPE_MAX_SIZE = 10;
31 }
32 
33 bool ExtractZipFile(ModuleZipHelper &helper, const std::string &fileName, std::string &buf);
34 bool ParseVersion(const std::string &version, const std::string &split, std::vector<std::string> &versionVec);
35 bool ComparePackInfoVer(const std::vector<std::string> &smallVersion, const std::vector<std::string> &bigVersion);
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 int32_t VerifyModulePackageSign(const std::string &path);
40 #ifdef __cplusplus
41 }
42 #endif
43 
44 struct ModuleVersion {
45     uint32_t apiVersion;
46     uint32_t versionCode;
47     uint32_t patchVersion;
48 
stringModuleVersion49     operator std::string() const
50     {
51         return std::to_string(apiVersion) + std::to_string(versionCode) + std::to_string(patchVersion);
52     }
53 };
54 
55 struct ImageStat {
56     uint32_t imageOffset;
57     uint32_t imageSize;
58     char fsType[FS_TYPE_MAX_SIZE];
59 };
60 
61 class ModuleFile {
62 public:
63     static std::unique_ptr<ModuleFile> Open(const std::string &path);
64     static bool CompareVersion(const ModuleFile &file1, const ModuleFile &file2);
ModuleFile(const std::string & modulePath,const std::string & saName,const int32_t saId,const ModuleVersion & versionInfo,const std::string & modulePubkey,const std::optional<ImageStat> & imageStat)65     ModuleFile(const std::string &modulePath,
66                const std::string &saName,
67                const int32_t saId,
68                const ModuleVersion &versionInfo,
69                const std::string &modulePubkey,
70                const std::optional<ImageStat> &imageStat)
71         : modulePath_(modulePath),
72           saName_(saName),
73           saId_(saId),
74           versionInfo_(versionInfo),
75           modulePubkey_(modulePubkey),
76           imageStat_(imageStat) {}
77     virtual ~ModuleFile() = default;
78     ModuleFile(const ModuleFile&) = default;
79     ModuleFile& operator=(const ModuleFile&) = default;
80     ModuleFile(ModuleFile&&) = default;
81     ModuleFile& operator=(ModuleFile&&) = default;
82 
GetPath()83     const std::string &GetPath() const
84     {
85         return modulePath_;
86     }
GetSaName()87     const std::string &GetSaName() const
88     {
89         return saName_;
90     }
GetSaId()91     int32_t GetSaId() const
92     {
93         return saId_;
94     }
GetVersionInfo()95     const ModuleVersion &GetVersionInfo() const
96     {
97         return versionInfo_;
98     }
GetPublicKey()99     const std::string &GetPublicKey() const
100     {
101         return modulePubkey_;
102     }
GetImageStat()103     const std::optional<ImageStat> &GetImageStat() const
104     {
105         return imageStat_;
106     }
SetPath(const std::string & path)107     void SetPath(const std::string &path)
108     {
109         modulePath_ = path;
110     }
111     bool VerifyModuleVerity(const std::string &publicKey);
112     void ClearVerifiedData();
113 #ifdef SUPPORT_HVB
GetVerifiedData()114     struct hvb_verified_data *GetVerifiedData() const
115     {
116         return vd_;
117     }
118 #endif
119 
120 private:
121     std::string modulePath_;
122     std::string saName_;
123     int32_t saId_ = 0;
124     ModuleVersion versionInfo_;
125     std::string modulePubkey_;
126     std::optional<ImageStat> imageStat_;
127 
128 #ifdef SUPPORT_HVB
129     struct hvb_verified_data *vd_ = nullptr;
130 #endif
131 };
132 } // namespace SysInstaller
133 } // namespace OHOS
134 #endif // SYS_INSTALLER_MODULE_FILE_H