• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 "hce_session_stub.h"
17 
18 #include "hce_cmd_death_recipient.h"
19 #include "ipc_skeleton.h"
20 #include "loghelper.h"
21 #include "nfc_sdk_common.h"
22 #include "nfc_service_ipc_interface_code.h"
23 #include "hce_cmd_callback_proxy.h"
24 #include "nfc_permission_checker.h"
25 #include "external_deps_proxy.h"
26 #include "ce_payment_services_parcelable.h"
27 #include "ability_info.h"
28 #include "accesstoken_kit.h"
29 #include "hap_token_info.h"
30 
31 namespace OHOS {
32 namespace NFC {
33 namespace HCE {
34 using AppExecFwk::AbilityInfo;
OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply,OHOS::MessageOption & option)35 int HceSessionStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply,
36                                     OHOS::MessageOption &option)
37 {
38     DebugLog("hceSessionStub OnRemoteRequest occur, code is %d", code);
39     if (data.ReadInterfaceToken() != GetDescriptor()) {
40         ErrorLog("hceSessionStub OnRemoteRequest GetDescriptor failed");
41         return KITS::ErrorCode::ERR_HCE_PARAMETERS;
42     }
43 
44     switch (code) {
45         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_ON):
46             return HandleRegHceCmdCallback(data, reply);
47         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_TRANSMIT):
48             return HandleSendRawFrame(data, reply);
49         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_GET_PAYMENT_SERVICES):
50             return HandleGetPaymentServices(data, reply);
51         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_STOP):
52             return HandleStopHce(data, reply);
53         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_IS_DEFAULT_SERVICE):
54             return HandleIsDefaultService(data, reply);
55         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_START):
56             return HandleStartHce(data, reply);
57         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_CE_HCE_OFF):
58             return HandleUnRegHceCmdCallback(data, reply);
59         default: return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60     }
61 }
62 
HandleRegHceCmdCallback(MessageParcel & data,MessageParcel & reply)63 int HceSessionStub::HandleRegHceCmdCallback(MessageParcel &data, MessageParcel &reply)
64 {
65     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
66         ErrorLog("HandleRegHceCmdCallback, ERR_NO_PERMISSION");
67         return KITS::ErrorCode::ERR_NO_PERMISSION;
68     }
69     std::string type = data.ReadString();
70     int exception = data.ReadInt32();
71     if (exception) {
72         return KITS::ERR_NFC_PARAMETERS;
73     }
74     KITS::ErrorCode ret = KITS::ERR_NFC_PARAMETERS;
75     do {
76         sptr<IRemoteObject> remote = data.ReadRemoteObject();
77         if (remote == nullptr) {
78             DebugLog("Failed to readRemoteObject!");
79             break;
80         }
81         std::unique_ptr<HceCmdDeathRecipient> recipient =
82             std::make_unique<HceCmdDeathRecipient>(this, IPCSkeleton::GetCallingTokenID());
83         sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
84         if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(dr))) {
85             ErrorLog("Failed to add death recipient");
86             return ERR_NONE;
87         }
88         {
89             std::lock_guard<std::mutex> guard(mutex_);
90             deathRecipient_ = dr;
91             hceCmdCallback_ = iface_cast<KITS::IHceCmdCallback>(remote);
92             if (hceCmdCallback_ == nullptr) {
93                 hceCmdCallback_ = new (std::nothrow) HceCmdCallbackProxy(remote);
94                 DebugLog("create new `HceCmdCallbackProxy`!");
95             }
96             ret = RegHceCmdCallback(hceCmdCallback_, type);
97         }
98     } while (0);
99     reply.WriteInt32(ret);
100     return ERR_NONE;
101 }
102 
HandleUnRegHceCmdCallback(MessageParcel & data,MessageParcel & reply)103 int HceSessionStub::HandleUnRegHceCmdCallback(MessageParcel &data, MessageParcel &reply)
104 {
105     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
106         ErrorLog("HandleUnRegHceCmdCallback, ERR_NO_PERMISSION");
107         return KITS::ErrorCode::ERR_NO_PERMISSION;
108     }
109     std::string type = data.ReadString();
110     int exception = data.ReadInt32();
111     if (exception) {
112         return KITS::ERR_NFC_PARAMETERS;
113     }
114     KITS::ErrorCode ret = KITS::ERR_NFC_PARAMETERS;
115     do {
116         sptr<IRemoteObject> remote = data.ReadRemoteObject();
117         if (remote == nullptr) {
118             DebugLog("Failed to readRemoteObject!");
119             break;
120         }
121         std::lock_guard<std::mutex> guard(mutex_);
122         if ((remote->IsProxyObject()) && (!remote->RemoveDeathRecipient(deathRecipient_))) {
123             ErrorLog("Failed to remove death recipient");
124             return ERR_NONE;
125         }
126         {
127             ret = UnregHceCmdCallback(nullptr, type);
128         }
129     } while (0);
130     reply.WriteInt32(ret);
131     return ERR_NONE;
132 }
133 
HandleSendRawFrame(OHOS::MessageParcel & data,OHOS::MessageParcel & reply)134 int HceSessionStub::HandleSendRawFrame(OHOS::MessageParcel &data, OHOS::MessageParcel &reply)
135 {
136     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
137         ErrorLog("HandleSendRawFrame, ERR_NO_PERMISSION");
138         return KITS::ErrorCode::ERR_NO_PERMISSION;
139     }
140 
141     std::string hexCmdData = data.ReadString();
142     if (hexCmdData.size() > KITS::MAX_APDU_DATA_HEX_STR) {
143         ErrorLog("raw frame too long");
144         return KITS::ErrorCode::ERR_HCE_PARAMETERS;
145     }
146     bool raw = data.ReadBool();
147     std::string hexRespData;
148     int statusCode = SendRawFrame(hexCmdData, raw, hexRespData);
149     reply.WriteString(hexRespData);
150     return statusCode;
151 }
HandleGetPaymentServices(MessageParcel & data,MessageParcel & reply)152 int HceSessionStub::HandleGetPaymentServices(MessageParcel &data, MessageParcel &reply)
153 {
154     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
155         ErrorLog("HandleGetPaymentServices, ERR_NO_PERMISSION");
156         return KITS::ErrorCode::ERR_NO_PERMISSION;
157     }
158 
159     if (!ExternalDepsProxy::GetInstance().IsSystemApp(IPCSkeleton::GetCallingUid())) {
160         ErrorLog("HandleGetPaymentServices, ERR_NOT_SYSTEM_APP");
161         return KITS::ErrorCode::ERR_NOT_SYSTEM_APP;
162     }
163     int exception = data.ReadInt32();
164     if (exception) {
165         ErrorLog("HandleGetPaymentServices, exception");
166         return KITS::ERR_NFC_PARAMETERS;
167     }
168     std::vector<AbilityInfo> abilityInfos;
169     int result = GetPaymentServices(abilityInfos);
170     if (result != NFC::KITS::ErrorCode::ERR_NONE) {
171         ErrorLog("HandleGetPaymentServices, get payment service failed");
172         return KITS::ErrorCode::ERR_HCE_NOT_GET_PAYMENT_SERVICES;
173     }
174     KITS::CePaymentServicesParcelable paymentServiceMsg;
175     paymentServiceMsg.paymentAbilityInfos = abilityInfos;
176     if (!reply.WriteParcelable(&paymentServiceMsg)) {
177         ErrorLog("HandleGetPaymentServices, write payment service failed");
178         return KITS::ErrorCode::ERR_HCE_PARAMETERS;
179     }
180     return ERR_NONE;
181 }
182 
HandleStopHce(MessageParcel & data,MessageParcel & reply)183 int HceSessionStub::HandleStopHce(MessageParcel &data, MessageParcel &reply)
184 {
185     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
186         ErrorLog("HandleStopHce, ERR_NO_PERMISSION");
187         return KITS::ErrorCode::ERR_NO_PERMISSION;
188     }
189     InfoLog("HandleStopHce");
190     ElementName *element = ElementName::Unmarshalling(data);
191     if (element == nullptr) {
192         ErrorLog("HandleStopHce, unmarshalled element is null");
193         return KITS::ERR_HCE_PARAMETERS;
194     }
195     int exception = data.ReadInt32();
196     if (exception) {
197         // element is newed by Unmarshalling, should be deleted
198         delete element;
199         element = nullptr;
200         return KITS::ERR_HCE_PARAMETERS;
201     }
202     KITS::ErrorCode ret = StopHce(*(element));
203     DebugLog("HandleStopHce end##ret=%{public}d\n", ret);
204     reply.WriteInt32(ret);
205 
206     // element is newed by Unmarshalling, should be deleted
207     delete element;
208     element = nullptr;
209     return ERR_NONE;
210 }
211 
HandleStartHce(MessageParcel & data,MessageParcel & reply)212 int HceSessionStub::HandleStartHce(MessageParcel &data, MessageParcel &reply)
213 {
214     if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
215         ErrorLog("HandleStartHce, ERR_NO_PERMISSION");
216         return KITS::ErrorCode::ERR_NO_PERMISSION;
217     }
218     InfoLog("HandleStartHce");
219     std::shared_ptr<KITS::StartHceInfoParcelable> startHceInfo = std::make_shared<KITS::StartHceInfoParcelable>(data);
220     if (startHceInfo == nullptr) {
221         ErrorLog("HandleStartHce, unmarshalled satrtHceInfo is null");
222         return KITS::ERR_HCE_PARAMETERS;
223     }
224     int exception = data.ReadInt32();
225     if (exception) {
226         ErrorLog("HandleStartHce, unmarshalled exception ");
227         return KITS::ERR_HCE_PARAMETERS;
228     }
229     KITS::ErrorCode ret = StartHceInner(startHceInfo);
230     DebugLog("HandleStartHce end##ret=%{public}d\n", ret);
231     reply.WriteInt32(ret);
232     return ERR_NONE;
233 }
234 
StartHceInner(std::shared_ptr<KITS::StartHceInfoParcelable> startHceInfo)235 KITS::ErrorCode HceSessionStub::StartHceInner(std::shared_ptr<KITS::StartHceInfoParcelable> startHceInfo)
236 {
237     Security::AccessToken::HapTokenInfo hapTokenInfo;
238     Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
239     int result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(callerToken, hapTokenInfo);
240 
241     InfoLog("get hap token info, result = %{public}d", result);
242     if (result) {
243         return KITS::ERR_HCE_PARAMETERS;
244     }
245     if (hapTokenInfo.bundleName.empty()) {
246         ErrorLog("StartHce: not got bundle name");
247         return KITS::ERR_HCE_PARAMETERS;
248     }
249     ElementName element = startHceInfo->GetElement();
250     std::vector<std::string> aids = startHceInfo->GetAids();
251     if (hapTokenInfo.bundleName != element.GetBundleName()) {
252         ErrorLog("StartHce: wrong bundle name");
253         return KITS::ERR_HCE_PARAMETERS;
254     }
255     return StartHce(element, aids);
256 }
257 
HandleIsDefaultService(MessageParcel & data,MessageParcel & reply)258 int HceSessionStub::HandleIsDefaultService(MessageParcel &data, MessageParcel &reply)
259 {
260     InfoLog("HandleIsDefaultService");
261     ElementName *element = ElementName::Unmarshalling(data);
262     if (element == nullptr) {
263         ErrorLog("HandleIsDefaultService, unmarshalled element is null");
264         return KITS::ERR_HCE_PARAMETERS;
265     }
266 
267     std::string type = data.ReadString();
268 
269     int exception = data.ReadInt32();
270     if (exception) {
271         // element is newed by Unmarshalling, should be deleted
272         delete element;
273         element = nullptr;
274         return KITS::ERR_HCE_PARAMETERS;
275     }
276     bool isDefaultService = false;
277     KITS::ErrorCode ret = IsDefaultService(*(element), type, isDefaultService);
278     DebugLog("HandleIsDefaultService end##ret=%{public}d\n", ret);
279     reply.WriteBool(isDefaultService);
280     // element is newed by Unmarshalling, should be deleted
281     delete element;
282     element = nullptr;
283     return ERR_NONE;
284 }
RegHceCmdCallback(const sptr<KITS::IHceCmdCallback> & callback,const std::string & type)285 KITS::ErrorCode HceSessionStub::RegHceCmdCallback(const sptr<KITS::IHceCmdCallback> &callback,
286                                                   const std::string &type)
287 {
288     return RegHceCmdCallbackByToken(callback, type, IPCSkeleton::GetCallingTokenID());
289 }
290 
UnregHceCmdCallback(const sptr<KITS::IHceCmdCallback> & callback,const std::string & type)291 KITS::ErrorCode HceSessionStub::UnregHceCmdCallback(
292     const sptr<KITS::IHceCmdCallback> &callback, const std::string &type)
293 {
294     return UnRegHceCmdCallbackByToken(type, IPCSkeleton::GetCallingTokenID());
295 }
296 
SendRawFrame(std::string hexCmdData,bool raw,std::string & hexRespData)297 int HceSessionStub::SendRawFrame(std::string hexCmdData, bool raw, std::string &hexRespData)
298 {
299     return SendRawFrameByToken(hexCmdData, raw, hexRespData, IPCSkeleton::GetCallingTokenID());
300 }
301 
StopHce(ElementName & element)302 KITS::ErrorCode HceSessionStub::StopHce(ElementName &element)
303 {
304     Security::AccessToken::HapTokenInfo hapTokenInfo;
305     Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
306     int result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(callerToken, hapTokenInfo);
307 
308     InfoLog("get hap token info, result = %{public}d", result);
309     if (result) {
310         return KITS::ERR_HCE_PARAMETERS;
311     }
312     if (hapTokenInfo.bundleName.empty()) {
313         ErrorLog("StopHce: not got bundle name");
314         return KITS::ERR_HCE_PARAMETERS;
315     }
316     if (hapTokenInfo.bundleName != element.GetBundleName()) {
317         ErrorLog("StopHce: wrong bundle name");
318         return KITS::ERR_HCE_PARAMETERS;
319     }
320 
321     return StopHce(element, IPCSkeleton::GetCallingTokenID());
322 }
323 
RemoveHceDeathRecipient(const wptr<IRemoteObject> & remote)324 void HceSessionStub::RemoveHceDeathRecipient(const wptr<IRemoteObject> &remote)
325 {
326     std::lock_guard<std::mutex> guard(mutex_);
327     if (hceCmdCallback_ == nullptr) {
328         ErrorLog("hce OnRemoteDied callback_ is nullptr");
329         return;
330     }
331     auto serviceRemote = hceCmdCallback_->AsObject();
332     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
333         serviceRemote->RemoveDeathRecipient(deathRecipient_);
334         hceCmdCallback_ = nullptr;
335         ErrorLog("hce on remote died");
336     }
337 }
338 } // namespace HCE
339 } // namespace NFC
340 } // namespace OHOS
341