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 }
56 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)57 int32_t El5FilekeyManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
58     MessageOption &option)
59 {
60     if (data.ReadInterfaceToken() != El5FilekeyManagerInterface::GetDescriptor()) {
61         LOG_ERROR("get unexpected descriptor");
62         return EFM_ERR_IPC_TOKEN_INVALID;
63     }
64 
65     auto itFunc = requestMap_.find(code);
66     if (itFunc != requestMap_.end()) {
67         auto requestFunc = itFunc->second;
68         if (requestFunc != nullptr) {
69             (this->*requestFunc)(data, reply);
70             return NO_ERROR;
71         }
72     }
73 
74     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
75 }
76 
AcquireAccessInner(MessageParcel & data,MessageParcel & reply)77 void El5FilekeyManagerStub::AcquireAccessInner(MessageParcel &data, MessageParcel &reply)
78 {
79     DataLockType type = static_cast<DataLockType>(data.ReadInt32());
80     reply.WriteInt32(this->AcquireAccess(type));
81 }
82 
ReleaseAccessInner(MessageParcel & data,MessageParcel & reply)83 void El5FilekeyManagerStub::ReleaseAccessInner(MessageParcel &data, MessageParcel &reply)
84 {
85     DataLockType type = static_cast<DataLockType>(data.ReadInt32());
86     reply.WriteInt32(this->ReleaseAccess(type));
87 }
88 
GenerateAppKeyInner(MessageParcel & data,MessageParcel & reply)89 void El5FilekeyManagerStub::GenerateAppKeyInner(MessageParcel &data, MessageParcel &reply)
90 {
91     uint32_t uid = data.ReadUint32();
92     std::string bundleName = data.ReadString();
93     std::string keyId;
94     reply.WriteInt32(this->GenerateAppKey(uid, bundleName, keyId));
95     reply.WriteString(keyId);
96 }
97 
DeleteAppKeyInner(MessageParcel & data,MessageParcel & reply)98 void El5FilekeyManagerStub::DeleteAppKeyInner(MessageParcel &data, MessageParcel &reply)
99 {
100     std::string bundleName = data.ReadString();
101     int32_t userId = data.ReadInt32();
102     reply.WriteInt32(this->DeleteAppKey(bundleName, userId));
103 }
104 
GetUserAppKeyInner(MessageParcel & data,MessageParcel & reply)105 void El5FilekeyManagerStub::GetUserAppKeyInner(MessageParcel &data, MessageParcel &reply)
106 {
107     int32_t userId = data.ReadInt32();
108     bool getAllFlag = data.ReadBool();
109     std::vector<std::pair<int32_t, std::string>> keyInfos;
110     reply.WriteInt32(this->GetUserAppKey(userId, getAllFlag, keyInfos));
111     MarshallingKeyInfos(reply, keyInfos);
112 }
113 
ChangeUserAppkeysLoadInfoInner(MessageParcel & data,MessageParcel & reply)114 void El5FilekeyManagerStub::ChangeUserAppkeysLoadInfoInner(MessageParcel &data, MessageParcel &reply)
115 {
116     int32_t userId = data.ReadInt32();
117     std::vector<std::pair<std::string, bool>> loadInfos;
118     int32_t ret = UnmarshallingLoadInfos(data, loadInfos);
119     if (ret == EFM_SUCCESS) {
120         ret = this->ChangeUserAppkeysLoadInfo(userId, loadInfos);
121     }
122     reply.WriteInt32(ret);
123 }
124 
SetFilePathPolicyInner(MessageParcel & data,MessageParcel & reply)125 void El5FilekeyManagerStub::SetFilePathPolicyInner(MessageParcel &data, MessageParcel &reply)
126 {
127     reply.WriteInt32(this->SetFilePathPolicy());
128 }
129 
RegisterCallbackInner(MessageParcel & data,MessageParcel & reply)130 void El5FilekeyManagerStub::RegisterCallbackInner(MessageParcel &data, MessageParcel &reply)
131 {
132     auto callback = iface_cast<El5FilekeyCallbackInterface>(data.ReadRemoteObject());
133     if ((callback == nullptr) || (!callback->AsObject())) {
134         LOG_ERROR("Read callback failed.");
135         reply.WriteInt32(EFM_ERR_IPC_READ_DATA);
136         return;
137     }
138     reply.WriteInt32(this->RegisterCallback(callback));
139 }
140 
MarshallingKeyInfos(MessageParcel & reply,std::vector<std::pair<int32_t,std::string>> & keyInfos)141 void El5FilekeyManagerStub::MarshallingKeyInfos(MessageParcel &reply,
142     std::vector<std::pair<int32_t, std::string>>& keyInfos)
143 {
144     reply.WriteUint32(keyInfos.size());
145     for (std::pair<int32_t, std::string> &keyInfo : keyInfos) {
146         reply.WriteInt32(keyInfo.first);
147         reply.WriteString(keyInfo.second);
148     }
149 }
150 
UnmarshallingLoadInfos(MessageParcel & data,std::vector<std::pair<std::string,bool>> & loadInfos)151 int32_t El5FilekeyManagerStub::UnmarshallingLoadInfos(MessageParcel &data,
152     std::vector<std::pair<std::string, bool>> &loadInfos)
153 {
154     uint32_t loadInfosSize = data.ReadUint32();
155     if (loadInfosSize > MAX_KEY_SIZE) {
156         LOG_ERROR("Parse loadInfos failed, results oversize %{public}d.", loadInfosSize);
157         return EFM_ERR_IPC_READ_DATA;
158     }
159     for (uint32_t i = 0; i < loadInfosSize; ++i) {
160         std::string keyId = data.ReadString();
161         bool loadState = data.ReadBool();
162         loadInfos.emplace_back(std::make_pair(keyId, loadState));
163     }
164     return EFM_SUCCESS;
165 }
166 }  // namespace AccessToken
167 }  // namespace Security
168 }  // namespace OHOS
169