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 namespace OHOS { 27 namespace SysInstaller { 28 namespace { 29 constexpr size_t FS_TYPE_MAX_SIZE = 10; 30 } 31 32 struct ModuleVersion { 33 uint32_t apiVersion; 34 uint32_t versionCode; 35 uint32_t patchVersion; 36 stringModuleVersion37 operator std::string() const 38 { 39 return std::to_string(apiVersion) + std::to_string(versionCode) + std::to_string(patchVersion); 40 } 41 }; 42 43 struct ImageStat { 44 uint32_t imageOffset; 45 uint32_t imageSize; 46 char fsType[FS_TYPE_MAX_SIZE]; 47 }; 48 49 class ModuleFile { 50 public: 51 static std::unique_ptr<ModuleFile> Open(const std::string &path); 52 static bool VerifyModulePackageSign(const std::string &path); 53 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)54 ModuleFile(const std::string &modulePath, 55 const std::string &saName, 56 const int32_t saId, 57 const ModuleVersion &versionInfo, 58 const std::string &modulePubkey, 59 const std::optional<ImageStat> &imageStat) 60 : modulePath_(modulePath), 61 saName_(saName), 62 saId_(saId), 63 versionInfo_(versionInfo), 64 modulePubkey_(modulePubkey), 65 imageStat_(imageStat) {} 66 virtual ~ModuleFile() = default; 67 ModuleFile(const ModuleFile&) = default; 68 ModuleFile& operator=(const ModuleFile&) = default; 69 ModuleFile(ModuleFile&&) = default; 70 ModuleFile& operator=(ModuleFile&&) = default; 71 GetPath()72 const std::string &GetPath() const 73 { 74 return modulePath_; 75 } GetSaName()76 const std::string &GetSaName() const 77 { 78 return saName_; 79 } GetSaId()80 int32_t GetSaId() const 81 { 82 return saId_; 83 } GetVersionInfo()84 const ModuleVersion &GetVersionInfo() const 85 { 86 return versionInfo_; 87 } GetPublicKey()88 const std::string &GetPublicKey() const 89 { 90 return modulePubkey_; 91 } GetImageStat()92 const std::optional<ImageStat> &GetImageStat() const 93 { 94 return imageStat_; 95 } SetPath(const std::string & path)96 void SetPath(const std::string &path) 97 { 98 modulePath_ = path; 99 } 100 bool VerifyModuleVerity(const std::string &publicKey); 101 void ClearVerifiedData(); 102 #ifdef SUPPORT_HVB GetVerifiedData()103 struct hvb_verified_data *GetVerifiedData() const 104 { 105 return vd_; 106 } 107 #endif 108 109 private: 110 std::string modulePath_; 111 std::string saName_; 112 int32_t saId_ = 0; 113 ModuleVersion versionInfo_; 114 std::string modulePubkey_; 115 std::optional<ImageStat> imageStat_; 116 117 #ifdef SUPPORT_HVB 118 struct hvb_verified_data *vd_ = nullptr; 119 #endif 120 }; 121 } // namespace SysInstaller 122 } // namespace OHOS 123 #endif // SYS_INSTALLER_MODULE_FILE_H