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 INTERFACES_INNER_API_DLP_FILE_H 17 #define INTERFACES_INNER_API_DLP_FILE_H 18 19 #include <string> 20 #include "dlp_crypt.h" 21 #include "permission_policy.h" 22 23 namespace OHOS { 24 namespace Security { 25 namespace DlpPermission { 26 static constexpr uint32_t INVALID_FILE_SIZE = 0xffffffff; 27 static constexpr uint32_t DLP_BUFF_LEN = 4096; 28 static constexpr uint32_t IV_SIZE = 16; 29 static constexpr uint32_t DLP_FILE_MAGIC = 0x87f4922; 30 static constexpr uint32_t DLP_FUSE_MAX_BUFFLEN = (10 * 1024 * 1024); // 10M 31 static constexpr uint32_t DLP_BLOCK_SIZE = 16; 32 // dlp file only support 32bits size, apart from 10M max head size 33 static constexpr uint32_t DLP_MAX_CONTENT_SIZE = 0xffffffff - 0xA00000; 34 static constexpr uint32_t HOLE_BUFF_SIZE = 16 * 1024; 35 static constexpr uint32_t HOLE_BUFF_SMALL_SIZE = 1 * 1024; 36 static constexpr uint32_t MAX_HOLE_SIZE = 50 * 1024 * 1024; // 50M 37 38 enum DlpOperation { 39 DLP_ENCRYPTION = 1, 40 DLP_DECRYPTION = 2, 41 }; 42 43 struct DlpCipher { 44 struct DlpBlob encKey; 45 struct DlpCipherParam tagIv; 46 struct DlpUsageSpec usageSpec; 47 }; 48 49 struct DlpHeader { 50 uint32_t magic; 51 uint32_t version; 52 uint32_t offlineAccess; 53 uint32_t txtOffset; 54 uint32_t txtSize; 55 uint32_t certOffset; 56 uint32_t certSize; 57 uint32_t contactAccountOffset; 58 uint32_t contactAccountSize; 59 uint32_t offlineCertOffset; 60 uint32_t offlineCertSize; 61 }; 62 63 enum VALID_KEY_SIZE { 64 DLP_KEY_LEN_128 = 16, 65 DLP_KEY_LEN_192 = 24, 66 DLP_KEY_LEN_256 = 32, 67 }; 68 69 class DlpFile { 70 public: 71 DlpFile(int32_t dlpFd); 72 ~DlpFile(); 73 74 int32_t SetCipher(const struct DlpBlob& key, const struct DlpUsageSpec& spec); 75 int32_t ParseDlpHeader(); 76 void GetEncryptCert(struct DlpBlob& cert) const; 77 void GetOfflineCert(struct DlpBlob& cert) const; 78 int32_t AddOfflineCert(std::vector<uint8_t>& offlineCert, const std::string& workDir); 79 int32_t SetEncryptCert(const struct DlpBlob& cert); 80 void SetOfflineAccess(bool flag); 81 bool GetOfflineAccess(); 82 int32_t GenFile(int32_t inPlainFileFd); 83 int32_t RemoveDlpPermission(int outPlainFileFd); 84 int32_t DlpFileRead(uint32_t offset, void* buf, uint32_t size); 85 int32_t DlpFileWrite(uint32_t offset, void* buf, uint32_t size); 86 uint32_t GetFsContentSize() const; 87 void UpdateDlpFilePermission(); 88 int32_t CheckDlpFile(); 89 90 int32_t SetPolicy(const PermissionPolicy& policy); GetPolicy(PermissionPolicy & policy)91 void GetPolicy(PermissionPolicy& policy) const 92 { 93 policy.CopyPermissionPolicy(policy_); 94 }; 95 96 int32_t SetContactAccount(const std::string& contactAccount); GetContactAccount(std::string & contactAccount)97 void GetContactAccount(std::string& contactAccount) const 98 { 99 contactAccount = contactAccount_; 100 }; 101 SetLinkStatus()102 void SetLinkStatus() 103 { 104 isFuseLink_ = true; 105 }; 106 RemoveLinkStatus()107 void RemoveLinkStatus() 108 { 109 isFuseLink_ = false; 110 }; 111 GetAuthPerm()112 DLPFileAccess GetAuthPerm() 113 { 114 return authPerm_; 115 }; 116 117 int32_t Truncate(uint32_t size); 118 int32_t dlpFd_; 119 120 private: 121 bool IsValidDlpHeader(const struct DlpHeader& head) const; 122 bool IsValidPadding(uint32_t padding); 123 bool IsValidCipher(const struct DlpBlob& key, const struct DlpUsageSpec& spec) const; 124 int32_t CopyBlobParam(const struct DlpBlob& src, struct DlpBlob& dst) const; 125 int32_t CleanBlobParam(struct DlpBlob& blob) const; 126 int32_t UpdateFileCertData(); 127 int32_t PrepareBuff(struct DlpBlob& message1, struct DlpBlob& message2) const; 128 int32_t GetLocalAccountName(std::string& account) const; 129 int32_t GetDomainAccountName(std::string& account) const; 130 int32_t DoDlpContentCryptyOperation(int32_t inFd, int32_t outFd, uint32_t inOffset, 131 uint32_t inFileLen, bool isEncrypt); 132 int32_t DoDlpContentCopyOperation(int32_t inFd, int32_t outFd, uint32_t inOffset, uint32_t inFileLen); 133 int32_t WriteHeadAndCert(int tmpFile, std::vector<uint8_t>& offlineCert); 134 int32_t DupUsageSpec(struct DlpUsageSpec& spec); 135 int32_t DoDlpBlockCryptOperation(struct DlpBlob& message1, 136 struct DlpBlob& message2, uint32_t offset, bool isEncrypt); 137 int32_t WriteFirstBlockData(uint32_t offset, void* buf, uint32_t size); 138 int32_t FillHoleData(uint32_t holeStart, uint32_t holeSize); 139 int32_t DoDlpFileWrite(uint32_t offset, void* buf, uint32_t size); 140 int32_t UpdateDlpFileContentSize(); 141 142 bool isFuseLink_; 143 DLPFileAccess authPerm_; 144 145 // dlp parse format 146 struct DlpHeader head_; 147 struct DlpBlob cert_; 148 struct DlpBlob offlineCert_; 149 struct DlpCipher cipher_; 150 151 // policy in certificate 152 PermissionPolicy policy_; 153 std::string contactAccount_; 154 }; 155 } // namespace DlpPermission 156 } // namespace Security 157 } // namespace OHOS 158 #endif /* INTERFACES_INNER_API_DLP_FILE_H */ 159