1 /* 2 * Copyright (c) 2023-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 #ifndef INTERFACES_INNER_API_DLP_FILE_MANAGER_H 17 #define INTERFACES_INNER_API_DLP_FILE_MANAGER_H 18 19 #include <atomic> 20 #include <mutex> 21 #include <unordered_map> 22 #include <string> 23 #include "dlp_crypt.h" 24 #include "dlp_file.h" 25 #include "permission_policy.h" 26 #include "rwlock.h" 27 28 namespace OHOS { 29 namespace Security { 30 namespace DlpPermission { 31 class DlpFileManager final { 32 public: 33 struct DlpFileMes { 34 int32_t plainFileFd; 35 int32_t dlpFileFd; 36 std::string realFileType; 37 }; 38 39 static DlpFileManager& GetInstance(); ~DlpFileManager()40 ~DlpFileManager() {}; 41 42 int32_t GenZipDlpFile(DlpFileMes& dlpFileMes, const DlpProperty& property, 43 std::shared_ptr<DlpFile>& filePtr, const std::string& workDir); 44 int32_t GenRawDlpFile(DlpFileMes& dlpFileMes, const DlpProperty& property, 45 std::shared_ptr<DlpFile>& filePtr); 46 47 int32_t GenerateDlpFile( 48 int32_t plainFileFd, int32_t dlpFileFd, const DlpProperty& property, std::shared_ptr<DlpFile>& filePtr, 49 const std::string& workDir); 50 51 int32_t OpenDlpFile(int32_t dlpFileFd, std::shared_ptr<DlpFile>& filePtr, const std::string& workDir, 52 const std::string& appId); 53 int32_t CloseDlpFile(const std::shared_ptr<DlpFile>& dlpFile); 54 int32_t RecoverDlpFile(std::shared_ptr<DlpFile>& file, int32_t plainFd) const; 55 int32_t SetDlpFileParams(std::shared_ptr<DlpFile>& filePtr, const DlpProperty& property) const; 56 int32_t DlpRawHmacCheckAndUpdata(std::shared_ptr<DlpFile>& filePtr, const std::vector<uint8_t>& offlineCert); 57 int32_t OpenRawDlpFile(int32_t dlpFileFd, std::shared_ptr<DlpFile>& filePtr, const std::string& appId, 58 const std::string& realType); 59 int32_t ParseZipDlpFileAndAddNode(std::shared_ptr<DlpFile>& filePtr, const std::string& appId); 60 int32_t OpenZipDlpFile(int32_t dlpFileFd, std::shared_ptr<DlpFile>& filePtr, const std::string& workDir, 61 const std::string& appId, const std::string& realType); 62 63 private: DlpFileManager()64 DlpFileManager() {}; 65 DISALLOW_COPY_AND_MOVE(DlpFileManager); 66 67 int32_t AddDlpFileNode(const std::shared_ptr<DlpFile>& filePtr); 68 int32_t RemoveDlpFileNode(const std::shared_ptr<DlpFile>& filePtr); 69 std::shared_ptr<DlpFile> GetDlpFile(int32_t dlpFd); 70 int32_t GenerateCertData(const PermissionPolicy& policy, struct DlpBlob& certData) const; 71 int32_t GenerateCertBlob(const std::vector<uint8_t>& cert, struct DlpBlob& certData) const; 72 int32_t UpdateDlpFile(const std::vector<uint8_t>& cert, std::shared_ptr<DlpFile>& filePtr); 73 int32_t PrepareDlpEncryptParms(PermissionPolicy& policy, struct DlpBlob& key, 74 struct DlpUsageSpec& usage, struct DlpBlob& certData, struct DlpBlob& hmacKey) const; 75 void FreeChiperBlob(struct DlpBlob& key, struct DlpBlob& certData, 76 struct DlpUsageSpec& usage, struct DlpBlob& hmacKey) const; 77 void CleanTempBlob(struct DlpBlob& key, struct DlpCipherParam** tagIv, struct DlpBlob& hmacKey) const; 78 std::mutex g_offlineLock_; 79 OHOS::Utils::RWLock g_DlpMapLock_; 80 std::unordered_map<int32_t, std::shared_ptr<DlpFile>> g_DlpFileMap_; 81 }; 82 } // namespace DlpPermission 83 } // namespace Security 84 } // namespace OHOS 85 #endif /* INTERFACES_INNER_API_DLP_FILE_MANAGER_H */ 86