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     static DlpFileManager& GetInstance();
~DlpFileManager()34     ~DlpFileManager() {};
35 
36     int32_t GenerateDlpFile(
37         int32_t plainFileFd, int32_t dlpFileFd, const DlpProperty& property, std::shared_ptr<DlpFile>& filePtr,
38         const std::string& workDir);
39     int32_t OpenDlpFile(int32_t dlpFileFd, std::shared_ptr<DlpFile>& filePtr, const std::string& workDir,
40         const std::string& appId);
41     int32_t CloseDlpFile(const std::shared_ptr<DlpFile>& dlpFile);
42     int32_t RecoverDlpFile(std::shared_ptr<DlpFile>& file, int32_t plainFd) const;
43 
44 private:
DlpFileManager()45     DlpFileManager() {};
46     DISALLOW_COPY_AND_MOVE(DlpFileManager);
47 
48     int32_t AddDlpFileNode(const std::shared_ptr<DlpFile>& filePtr);
49     int32_t RemoveDlpFileNode(const std::shared_ptr<DlpFile>& filePtr);
50     std::shared_ptr<DlpFile> GetDlpFile(int32_t dlpFd);
51     int32_t GenerateCertData(const PermissionPolicy& policy, struct DlpBlob& certData) const;
52     int32_t GenerateCertBlob(const std::vector<uint8_t>& cert, struct DlpBlob& certData) const;
53     int32_t UpdateDlpFile(bool isNeedAdapter, uint32_t oldCertSize, const std::string& workDir,
54         const std::vector<uint8_t>& cert, std::shared_ptr<DlpFile>& filePtr);
55     int32_t PrepareDlpEncryptParms(PermissionPolicy& policy, struct DlpBlob& key,
56         struct DlpUsageSpec& usage, struct DlpBlob& certData, struct DlpBlob& hmacKey) const;
57     int32_t ParseDlpFileFormat(std::shared_ptr<DlpFile>& filePtr, const std::string& workDir, const std::string& appId);
58     void FreeChiperBlob(struct DlpBlob& key, struct DlpBlob& certData,
59         struct DlpUsageSpec& usage, struct DlpBlob& hmacKey) const;
60     void CleanTempBlob(struct DlpBlob& key, struct DlpCipherParam** tagIv, struct DlpBlob& hmacKey) const;
61     int32_t SetDlpFileParams(std::shared_ptr<DlpFile>& filePtr, const DlpProperty& property) const;
62     std::mutex g_offlineLock_;
63     OHOS::Utils::RWLock g_DlpMapLock_;
64     std::unordered_map<int32_t, std::shared_ptr<DlpFile>> g_DlpFileMap_;
65 };
66 }  // namespace DlpPermission
67 }  // namespace Security
68 }  // namespace OHOS
69 #endif /*  INTERFACES_INNER_API_DLP_FILE_MANAGER_H */
70