1 /* 2 * Copyright (c) 2025-2025 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 STORAGE_DAEMON_CRYPTO_KEYMANAGER_EXT_H 17 #define STORAGE_DAEMON_CRYPTO_KEYMANAGER_EXT_H 18 19 #include <mutex> 20 21 #include "nocopyable.h" 22 23 #include "base_key.h" 24 #include "utils/file_utils.h" 25 26 namespace OHOS { 27 namespace StorageDaemon { 28 29 class UserkeyExtInterface { 30 public: 31 virtual ~UserkeyExtInterface() = default; 32 virtual int32_t GenerateUserKey(int32_t userId, const std::vector<uint8_t>& keyInfo) = 0; 33 virtual int32_t ActiveUserKey(int32_t userId, const std::vector<uint8_t>& keyInfo, 34 const std::vector<uint8_t>& token) = 0; 35 virtual int32_t InactiveUserKey(int32_t userId) = 0; 36 virtual int32_t DeleteUserKey(int32_t userId) = 0; 37 virtual int32_t SetFilePathPolicy(int32_t userId) = 0; 38 virtual int32_t SetRecoverKey(int32_t userId, const std::vector<uint8_t>& keyInfo) = 0; 39 }; 40 41 class KeyManagerExt { 42 public: GetInstance(void)43 static KeyManagerExt &GetInstance(void) 44 { 45 static KeyManagerExt instance; 46 return instance; 47 } 48 49 int GenerateUserKeys(uint32_t userId, uint32_t flags); 50 int DeleteUserKeys(uint32_t userId); 51 int ActiveUserKey(uint32_t userId, const std::vector<uint8_t>& token, 52 const std::vector<uint8_t>& secret); 53 int InActiveUserKey(uint32_t userId); 54 int SetRecoverKey(uint32_t userId, uint32_t keyType, const KeyBlob& ivBlob); 55 56 private: 57 KeyManagerExt(); 58 ~KeyManagerExt(); 59 60 int GetHashKey(uint32_t userId, KeyType type, KeyBlob& hashKey); 61 void Init(); 62 void UnInit(); 63 int DoDeleteUserKeys(uint32_t userId); 64 int DoActiveUserKey(uint32_t userId, 65 const std::vector<uint8_t>& token, 66 const std::vector<uint8_t>& secret); 67 int DoInactiveUserKey(uint32_t userId); 68 int GenerateAndInstallUserKey(uint32_t userId); IsServiceExtSoLoaded()69 bool IsServiceExtSoLoaded() { return service_ != nullptr; } SetMockService(UserkeyExtInterface * mockService)70 void SetMockService(UserkeyExtInterface* mockService) { service_ = mockService; } 71 72 std::mutex keyMutex_; 73 UserkeyExtInterface* service_ = nullptr; 74 void *handler_ = nullptr; 75 }; 76 } // namespace StorageDaemon 77 } // namespace OHOS 78 79 #endif // STORAGE_DAEMON_CRYPTO_KEYMANAGER_EXT_H 80