• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "el5_filekey_manager_stub.h"
17 
18 #include "el5_filekey_manager_error.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 namespace {
24 constexpr uint32_t MAX_KEY_SIZE = 1000;
25 }
26 
El5FilekeyManagerStub()27 El5FilekeyManagerStub::El5FilekeyManagerStub()
28 {
29     SetFuncInMap();
30 }
31 
~El5FilekeyManagerStub()32 El5FilekeyManagerStub::~El5FilekeyManagerStub()
33 {
34     requestMap_.clear();
35 }
36 
SetFuncInMap()37 void El5FilekeyManagerStub::SetFuncInMap()
38 {
39     requestMap_[static_cast<uint32_t>(EFMInterfaceCode::GENERATE_APP_KEY)] =
40         &El5FilekeyManagerStub::GenerateAppKeyInner;
41     requestMap_[static_cast<uint32_t>(EFMInterfaceCode::DELETE_APP_KEY)] =
42         &El5FilekeyManagerStub::DeleteAppKeyInner;
43     requestMap_[static_cast<uint32_t>(EFMInterfaceCode::ACQUIRE_ACCESS)] =
44         &El5FilekeyManagerStub::AcquireAccessInner;
45     requestMap_[static_cast<uint32_t>(EFMInterfaceCode::RELEASE_ACCESS)] =
46         &El5FilekeyManagerStub::ReleaseAccessInner;
47     requestMap_[static_cast<uint32_t>(EFMInterfaceCode::GET_USER_APP_KEY)] =
48         &El5FilekeyManagerStub::GetUserAppKeyInner;
49     requestMap_[static_cast<uint32_t>(EFMInterfaceCode::CHANGE_USER_APP_KEYS_LOAD_INFO)] =
50         &El5FilekeyManagerStub::ChangeUserAppkeysLoadInfoInner;
51     requestMap_[static_cast<uint32_t>(EFMInterfaceCode::SET_FILE_PATH_POLICY)] =
52         &El5FilekeyManagerStub::SetFilePathPolicyInner;
53     requestMap_[static_cast<uint32_t>(EFMInterfaceCode::REGISTER_CALLBACK)] =
54         &El5FilekeyManagerStub::RegisterCallbackInner;
55     requestMap_[static_cast<uint32_t>(EFMInterfaceCode::GENERATE_GROUPID_KEY)] =
56         &El5FilekeyManagerStub::GenerateGroupIDKeyInner;
57     requestMap_[static_cast<uint32_t>(EFMInterfaceCode::DELETE_GROUPID_KEY)] =
58         &El5FilekeyManagerStub::DeleteGroupIDKeyInner;
59     requestMap_[static_cast<uint32_t>(EFMInterfaceCode::QUERY_APP_KEY_STATE)] =
60         &El5FilekeyManagerStub::QueryAppKeyStateInner;
61 }
62 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)63 int32_t El5FilekeyManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
64     MessageOption &option)
65 {
66     if (data.ReadInterfaceToken() != El5FilekeyManagerInterface::GetDescriptor()) {
67         LOG_ERROR("Get unexpected descriptor");
68         return EFM_ERR_IPC_TOKEN_INVALID;
69     }
70 
71     auto itFunc = requestMap_.find(code);
72     if (itFunc != requestMap_.end()) {
73         auto requestFunc = itFunc->second;
74         if (requestFunc != nullptr) {
75             (this->*requestFunc)(data, reply);
76             return NO_ERROR;
77         }
78     }
79 
80     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
81 }
82 
AcquireAccessInner(MessageParcel & data,MessageParcel & reply)83 void El5FilekeyManagerStub::AcquireAccessInner(MessageParcel &data, MessageParcel &reply)
84 {
85     DataLockType type = static_cast<DataLockType>(data.ReadInt32());
86     reply.WriteInt32(this->AcquireAccess(type));
87 }
88 
ReleaseAccessInner(MessageParcel & data,MessageParcel & reply)89 void El5FilekeyManagerStub::ReleaseAccessInner(MessageParcel &data, MessageParcel &reply)
90 {
91     DataLockType type = static_cast<DataLockType>(data.ReadInt32());
92     reply.WriteInt32(this->ReleaseAccess(type));
93 }
94 
GenerateAppKeyInner(MessageParcel & data,MessageParcel & reply)95 void El5FilekeyManagerStub::GenerateAppKeyInner(MessageParcel &data, MessageParcel &reply)
96 {
97     uint32_t uid = data.ReadUint32();
98     std::string bundleName = data.ReadString();
99     std::string keyId;
100     reply.WriteInt32(this->GenerateAppKey(uid, bundleName, keyId));
101     reply.WriteString(keyId);
102 }
103 
DeleteAppKeyInner(MessageParcel & data,MessageParcel & reply)104 void El5FilekeyManagerStub::DeleteAppKeyInner(MessageParcel &data, MessageParcel &reply)
105 {
106     std::string bundleName = data.ReadString();
107     int32_t userId = data.ReadInt32();
108     reply.WriteInt32(this->DeleteAppKey(bundleName, userId));
109 }
110 
GetUserAppKeyInner(MessageParcel & data,MessageParcel & reply)111 void El5FilekeyManagerStub::GetUserAppKeyInner(MessageParcel &data, MessageParcel &reply)
112 {
113     int32_t userId = data.ReadInt32();
114     bool getAllFlag = data.ReadBool();
115     std::vector<std::pair<int32_t, std::string>> keyInfos;
116     reply.WriteInt32(this->GetUserAppKey(userId, getAllFlag, keyInfos));
117     MarshallingKeyInfos(reply, keyInfos);
118 }
119 
ChangeUserAppkeysLoadInfoInner(MessageParcel & data,MessageParcel & reply)120 void El5FilekeyManagerStub::ChangeUserAppkeysLoadInfoInner(MessageParcel &data, MessageParcel &reply)
121 {
122     int32_t userId = data.ReadInt32();
123     std::vector<std::pair<std::string, bool>> loadInfos;
124     int32_t ret = UnmarshallingLoadInfos(data, loadInfos);
125     if (ret == EFM_SUCCESS) {
126         ret = this->ChangeUserAppkeysLoadInfo(userId, loadInfos);
127     }
128     reply.WriteInt32(ret);
129 }
130 
SetFilePathPolicyInner(MessageParcel & data,MessageParcel & reply)131 void El5FilekeyManagerStub::SetFilePathPolicyInner(MessageParcel &data, MessageParcel &reply)
132 {
133     reply.WriteInt32(this->SetFilePathPolicy());
134 }
135 
RegisterCallbackInner(MessageParcel & data,MessageParcel & reply)136 void El5FilekeyManagerStub::RegisterCallbackInner(MessageParcel &data, MessageParcel &reply)
137 {
138     auto callback = iface_cast<El5FilekeyCallbackInterface>(data.ReadRemoteObject());
139     if ((callback == nullptr) || (!callback->AsObject())) {
140         LOG_ERROR("Read callback failed.");
141         reply.WriteInt32(EFM_ERR_IPC_READ_DATA);
142         return;
143     }
144     reply.WriteInt32(this->RegisterCallback(callback));
145 }
146 
GenerateGroupIDKeyInner(MessageParcel & data,MessageParcel & reply)147 void El5FilekeyManagerStub::GenerateGroupIDKeyInner(MessageParcel &data, MessageParcel &reply)
148 {
149     uint32_t uid = data.ReadUint32();
150     std::string groupID = data.ReadString();
151     std::string keyId;
152     reply.WriteInt32(this->GenerateGroupIDKey(uid, groupID, keyId));
153     reply.WriteString(keyId);
154 }
155 
DeleteGroupIDKeyInner(MessageParcel & data,MessageParcel & reply)156 void El5FilekeyManagerStub::DeleteGroupIDKeyInner(MessageParcel &data, MessageParcel &reply)
157 {
158     uint32_t uid = data.ReadUint32();
159     std::string groupID = data.ReadString();
160     reply.WriteInt32(this->DeleteGroupIDKey(uid, groupID));
161 }
162 
QueryAppKeyStateInner(MessageParcel & data,MessageParcel & reply)163 void El5FilekeyManagerStub::QueryAppKeyStateInner(MessageParcel &data, MessageParcel &reply)
164 {
165     DataLockType type = static_cast<DataLockType>(data.ReadInt32());
166     reply.WriteInt32(this->QueryAppKeyState(type));
167 }
168 
MarshallingKeyInfos(MessageParcel & reply,std::vector<std::pair<int32_t,std::string>> & keyInfos)169 void El5FilekeyManagerStub::MarshallingKeyInfos(MessageParcel &reply,
170     std::vector<std::pair<int32_t, std::string>>& keyInfos)
171 {
172     reply.WriteUint32(keyInfos.size());
173     for (std::pair<int32_t, std::string> &keyInfo : keyInfos) {
174         reply.WriteInt32(keyInfo.first);
175         reply.WriteString(keyInfo.second);
176     }
177 }
178 
UnmarshallingLoadInfos(MessageParcel & data,std::vector<std::pair<std::string,bool>> & loadInfos)179 int32_t El5FilekeyManagerStub::UnmarshallingLoadInfos(MessageParcel &data,
180     std::vector<std::pair<std::string, bool>> &loadInfos)
181 {
182     uint32_t loadInfosSize = data.ReadUint32();
183     if (loadInfosSize > MAX_KEY_SIZE) {
184         LOG_ERROR("Parse loadInfos failed, results oversize %{public}d.", loadInfosSize);
185         return EFM_ERR_IPC_READ_DATA;
186     }
187     for (uint32_t i = 0; i < loadInfosSize; ++i) {
188         std::string keyId = data.ReadString();
189         bool loadState = data.ReadBool();
190         loadInfos.emplace_back(std::make_pair(keyId, loadState));
191     }
192     return EFM_SUCCESS;
193 }
194 }  // namespace AccessToken
195 }  // namespace Security
196 }  // namespace OHOS
197