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 #include "crypto/filesystem_crypto.h"
17 #include "os_account_constants.h"
18 #include "storage_daemon_communication/storage_daemon_communication.h"
19 #include "storage_service_log.h"
20 #include "storage_service_errno.h"
21
22 namespace OHOS {
23 namespace StorageManager {
FileSystemCrypto()24 FileSystemCrypto::FileSystemCrypto()
25 {
26 LOGI("DEBUG FileSystemCrypto constructer");
27 }
28
~FileSystemCrypto()29 FileSystemCrypto::~FileSystemCrypto()
30 {
31 LOGI("DEBUG ~FileSystemCrypto destructer ~");
32 }
33
CheckUserIdRange(int32_t userId)34 int32_t FileSystemCrypto::CheckUserIdRange(int32_t userId)
35 {
36 if (userId < AccountSA::Constants::START_USER_ID || userId > AccountSA::Constants::MAX_USER_ID) {
37 LOGE("FileSystemCrypto: userId:%{public}d is out of range", userId);
38 return E_USERID_RANGE;
39 }
40 return E_OK;
41 }
42
GenerateUserKeys(uint32_t userId,uint32_t flags)43 int32_t FileSystemCrypto::GenerateUserKeys(uint32_t userId, uint32_t flags)
44 {
45 LOGI("UserId: %{public}u, flags: %{public}u", userId, flags);
46 int32_t err = CheckUserIdRange(userId);
47 if (err != E_OK) {
48 LOGE("User ID out of range");
49 return err;
50 }
51 std::shared_ptr<StorageDaemonCommunication> sdCommunication;
52 sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
53 err = sdCommunication->GenerateUserKeys(userId, flags);
54 return err;
55 }
56
DeleteUserKeys(uint32_t userId)57 int32_t FileSystemCrypto::DeleteUserKeys(uint32_t userId)
58 {
59 LOGI("UserId: %{public}u", userId);
60 int32_t err = CheckUserIdRange(userId);
61 if (err != E_OK) {
62 LOGE("User ID out of range");
63 return err;
64 }
65 std::shared_ptr<StorageDaemonCommunication> sdCommunication;
66 sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
67 err = sdCommunication->DeleteUserKeys(userId);
68 return err;
69 }
70
UpdateUserAuth(uint32_t userId,std::string auth,std::string compSecret)71 int32_t FileSystemCrypto::UpdateUserAuth(uint32_t userId, std::string auth, std::string compSecret)
72 {
73 LOGI("UserId: %{public}u", userId);
74 int32_t err = CheckUserIdRange(userId);
75 if (err != E_OK) {
76 LOGE("User ID out of range");
77 return err;
78 }
79 std::shared_ptr<StorageDaemonCommunication> sdCommunication;
80 sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
81 err = sdCommunication->UpdateUserAuth(userId, auth, compSecret);
82 return err;
83 }
84
ActiveUserKey(uint32_t userId,std::string auth,std::string compSecret)85 int32_t FileSystemCrypto::ActiveUserKey(uint32_t userId, std::string auth, std::string compSecret)
86 {
87 LOGI("UserId: %{public}u", userId);
88 int32_t err = CheckUserIdRange(userId);
89 if (err != E_OK) {
90 LOGE("User ID out of range");
91 return err;
92 }
93 std::shared_ptr<StorageDaemonCommunication> sdCommunication;
94 sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
95 err = sdCommunication->ActiveUserKey(userId, auth, compSecret);
96 return err;
97 }
98
InactiveUserKey(uint32_t userId)99 int32_t FileSystemCrypto::InactiveUserKey(uint32_t userId)
100 {
101 LOGI("UserId: %{public}u", userId);
102 int32_t err = CheckUserIdRange(userId);
103 if (err != E_OK) {
104 LOGE("User ID out of range");
105 return err;
106 }
107 std::shared_ptr<StorageDaemonCommunication> sdCommunication;
108 sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
109 err = sdCommunication->InactiveUserKey(userId);
110 return err;
111 }
112
UpdateKeyContext(uint32_t userId)113 int32_t FileSystemCrypto::UpdateKeyContext(uint32_t userId)
114 {
115 LOGI("UserId: %{public}u", userId);
116 int32_t err = CheckUserIdRange(userId);
117 if (err != E_OK) {
118 LOGE("User ID out of range");
119 return err;
120 }
121 std::shared_ptr<StorageDaemonCommunication> sdCommunication;
122 sdCommunication = DelayedSingleton<StorageDaemonCommunication>::GetInstance();
123 err = sdCommunication->UpdateKeyContext(userId);
124 return err;
125 }
126 }
127 }