• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "storage_service_constant.h"
25 #include "key_blob.h"
26 #include "base_key.h"
27 #include "utils/file_utils.h"
28 
29 namespace OHOS {
30 namespace StorageDaemon {
31 class KeyManager {
32 public:
GetInstance(void)33     static KeyManager *GetInstance(void)
34     {
35         static KeyManager instance;
36         return &instance;
37     }
38     int InitGlobalDeviceKey(void);
39     int InitGlobalUserKeys(void);
40     int GenerateUserKeys(unsigned int user, uint32_t flags);
41     int DeleteUserKeys(unsigned int user);
42     int UpdateUserAuth(unsigned int user, uint64_t secureUid,
43                        const std::vector<uint8_t> &token,
44                        const std::vector<uint8_t> &oldSecret,
45                        const std::vector<uint8_t> &newSecret);
46     int ActiveUserKey(unsigned int user, const std::vector<uint8_t> &token,
47                       const std::vector<uint8_t> &secret);
48     int InActiveUserKey(unsigned int user);
49     int SetDirectoryElPolicy(unsigned int user, KeyType type,
50                              const std::vector<FileList> &vec);
51     int UpdateKeyContext(uint32_t userId);
52 
53 private:
KeyManager()54     KeyManager()
55     {
56         hasGlobalDeviceKey_ = false;
57     }
~KeyManager()58     ~KeyManager() {}
59     int GenerateAndInstallDeviceKey(const std::string &dir);
60     int RestoreDeviceKey(const std::string &dir);
61     int GenerateAndInstallUserKey(uint32_t userId, const std::string &dir, const UserAuth &auth, KeyType type);
62     int RestoreUserKey(uint32_t userId, const std::string &dir, const UserAuth &auth, KeyType type);
63     int LoadAllUsersEl1Key(void);
64     int InitUserElkeyStorageDir(void);
65     bool HasElkey(uint32_t userId, KeyType type);
66     int DoDeleteUserKeys(unsigned int user);
67     int UpgradeKeys(const std::vector<FileList> &dirInfo);
68     std::shared_ptr<BaseKey> GetBaseKey(const std::string& dir);
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