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