1 /* 2 * Copyright (c) 2021 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_H 17 #define STORAGE_DAEMON_CRYPTO_KEYMANAGER_H 18 19 #include <iostream> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 24 #include "key_utils.h" 25 #include "base_key.h" 26 #include "utils/file_utils.h" 27 28 namespace OHOS { 29 namespace StorageDaemon { 30 constexpr uint32_t GLOBAL_USER_ID = 0; 31 enum KeyType { 32 EL1_KEY = 1, 33 EL2_KEY = 2, 34 }; 35 36 class KeyManager { 37 public: GetInstance(void)38 static KeyManager *GetInstance(void) 39 { 40 static KeyManager instance; 41 return &instance; 42 } 43 int InitGlobalDeviceKey(void); 44 int InitGlobalUserKeys(void); 45 int GenerateUserKeys(unsigned int user, uint32_t flags); 46 int DeleteUserKeys(unsigned int user); 47 int UpdateUserAuth(unsigned int user, const std::string &token, const std::string &composePwd); 48 int ActiveUserKey(unsigned int user, const std::string &token, 49 const std::string &secret); 50 int InActiveUserKey(unsigned int user); 51 int SetDirectoryElPolicy(unsigned int user, KeyType type, 52 const std::vector<FileList> &vec); 53 int UpdateKeyContext(uint32_t userId); 54 55 private: KeyManager()56 KeyManager() 57 { 58 hasGlobalDeviceKey_ = false; 59 } ~KeyManager()60 ~KeyManager() {} 61 int GenerateAndInstallDeviceKey(const std::string &dir); 62 int RestoreDeviceKey(const std::string &dir); 63 int GenerateAndInstallUserKey(uint32_t userId, const std::string &dir, const UserAuth &auth, KeyType type); 64 int RestoreUserKey(uint32_t userId, const std::string &dir, const UserAuth &auth, KeyType type); 65 int LoadAllUsersEl1Key(void); 66 int InitUserElkeyStorageDir(void); 67 bool HasElkey(uint32_t userId, KeyType type); 68 void DoDeleteUserKeys(unsigned int user); 69 70 std::map<unsigned int, std::shared_ptr<BaseKey>> userEl1Key_; 71 std::map<unsigned int, std::shared_ptr<BaseKey>> userEl2Key_; 72 std::shared_ptr<BaseKey> globalEl1Key_ { nullptr }; 73 74 std::mutex keyMutex_; 75 bool hasGlobalDeviceKey_; 76 }; 77 } // namespace StorageDaemon 78 } // namespace OHOS 79 80 #endif // STORAGE_DAEMON_CRYPTO_KEYMANAGER_H 81