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 #include "client/storage_manager_client.h"
16
17 #include "iremote_object.h"
18 #include "iremote_proxy.h"
19 #include "iservice_registry.h"
20 #include "storage_service_log.h"
21 #include "system_ability_definition.h"
22
23 namespace OHOS {
24 namespace StorageManager {
GetStorageManagerProxy(void)25 sptr<IStorageManager> StorageManagerClient::GetStorageManagerProxy(void)
26 {
27 auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
28 if (samgr == nullptr) {
29 LOGE("samgr empty error");
30 return nullptr;
31 }
32
33 sptr<IRemoteObject> object = samgr->GetSystemAbility(OHOS::STORAGE_MANAGER_MANAGER_ID);
34 if (object == nullptr) {
35 LOGE("storage manager client samgr ablity empty error");
36 return nullptr;
37 }
38
39 return iface_cast<IStorageManager>(object);
40 }
41
PrepareAddUser(uint32_t userId,uint32_t flags)42 int32_t StorageManagerClient::PrepareAddUser(uint32_t userId, uint32_t flags)
43 {
44 sptr<IStorageManager> client = GetStorageManagerProxy();
45 if (client == nullptr) {
46 LOGE("get storage manager service failed");
47 return -EFAULT;
48 }
49
50 return client->PrepareAddUser(userId, flags);
51 }
52
RemoveUser(uint32_t userId,uint32_t flags)53 int32_t StorageManagerClient::RemoveUser(uint32_t userId, uint32_t flags)
54 {
55 sptr<IStorageManager> client = GetStorageManagerProxy();
56 if (client == nullptr) {
57 LOGE("get storage manager service failed");
58 return -EFAULT;
59 }
60
61 return client->RemoveUser(userId, flags);
62 }
63
GenerateUserKeys(uint32_t userId,uint32_t flags)64 int32_t StorageManagerClient::GenerateUserKeys(uint32_t userId, uint32_t flags)
65 {
66 sptr<IStorageManager> client = GetStorageManagerProxy();
67 if (client == nullptr) {
68 LOGE("get storage manager service failed");
69 return -EFAULT;
70 }
71
72 return client->GenerateUserKeys(userId, flags);
73 }
74
DeleteUserKeys(uint32_t userId)75 int32_t StorageManagerClient::DeleteUserKeys(uint32_t userId)
76 {
77 sptr<IStorageManager> client = GetStorageManagerProxy();
78 if (client == nullptr) {
79 LOGE("get storage manager service failed");
80 return -EFAULT;
81 }
82
83 return client->DeleteUserKeys(userId);
84 }
85
UpdateUserAuth(uint32_t userId,uint64_t secureUid,const std::vector<uint8_t> & token,const std::vector<uint8_t> & oldSecret,const std::vector<uint8_t> & newSecret)86 int32_t StorageManagerClient::UpdateUserAuth(uint32_t userId, uint64_t secureUid,
87 const std::vector<uint8_t> &token,
88 const std::vector<uint8_t> &oldSecret,
89 const std::vector<uint8_t> &newSecret)
90 {
91 sptr<IStorageManager> client = GetStorageManagerProxy();
92 if (client == nullptr) {
93 LOGE("get storage manager service failed");
94 return -EFAULT;
95 }
96
97 return client->UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret);
98 }
99
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)100 int32_t StorageManagerClient::ActiveUserKey(uint32_t userId,
101 const std::vector<uint8_t> &token,
102 const std::vector<uint8_t> &secret)
103 {
104 sptr<IStorageManager> client = GetStorageManagerProxy();
105 if (client == nullptr) {
106 LOGE("get storage manager service failed");
107 return -EFAULT;
108 }
109
110 return client->ActiveUserKey(userId, token, secret);
111 }
112
InactiveUserKey(uint32_t userId)113 int32_t StorageManagerClient::InactiveUserKey(uint32_t userId)
114 {
115 sptr<IStorageManager> client = GetStorageManagerProxy();
116 if (client == nullptr) {
117 LOGE("get storage manager service failed");
118 return -EFAULT;
119 }
120
121 return client->InactiveUserKey(userId);
122 }
123
UpdateKeyContext(uint32_t userId)124 int32_t StorageManagerClient::UpdateKeyContext(uint32_t userId)
125 {
126 sptr<IStorageManager> client = GetStorageManagerProxy();
127 if (client == nullptr) {
128 LOGE("get storage manager service failed");
129 return -EFAULT;
130 }
131
132 return client->UpdateKeyContext(userId);
133 }
134 }
135 }
136