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 <memory>
17 #include "app_domain_verify_agent_service_stub.h"
18 #include "agent_interface_code.h"
19 #include "errors.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 #include "app_domain_verify_parcel_util.h"
23 
24 namespace OHOS {
25 namespace AppDomainVerify {
26 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)27 int32_t AppDomainVerifyAgentServiceStub::OnRemoteRequest(
28     uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
29 {
30     APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "onRemoteRequest##code = %{public}u", code);
31     std::u16string myDescripter = AppDomainVerifyAgentServiceStub::GetDescriptor();
32     std::u16string remoteDescripter = data.ReadInterfaceToken();
33     if (myDescripter != remoteDescripter) {
34         APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "end##descriptor checked fail");
35         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
36     }
37     switch (code) {
38         case static_cast<uint32_t>(static_cast<uint32_t>(AgentInterfaceCode::SINGLE_VERIFY)):
39             return OnSingleVerify(data, reply);
40         case static_cast<uint32_t>(AgentInterfaceCode::CONVERT_TO_EXPLICIT_WANT):
41             return OnConvertToExplicitWant(data, reply);
42         default:
43             APP_DOMAIN_VERIFY_HILOGW(
44                 APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "receive unknown code, code = %{public}d", code);
45             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
46     }
47 }
48 
OnSingleVerify(MessageParcel & data,MessageParcel & reply)49 int32_t AppDomainVerifyAgentServiceStub::OnSingleVerify(MessageParcel& data, MessageParcel& reply)
50 {
51     APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "%s called", __func__);
52     std::unique_ptr<AppVerifyBaseInfo> info(data.ReadParcelable<AppVerifyBaseInfo>());
53     if (!info) {
54         APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "read parcelable AppVerifyBaseInfo failed.");
55         return ERR_INVALID_VALUE;
56     }
57     AppVerifyBaseInfo appVerifyBaseInfo = *info;
58 
59     std::unique_ptr<VerifyResultInfo> result(data.ReadParcelable<VerifyResultInfo>());
60     if (!result) {
61         APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "read parcelable VerifyResultInfo failed.");
62         return ERR_INVALID_VALUE;
63     }
64     VerifyResultInfo verifyBaseInfo = *result;
65 
66     SingleVerify(appVerifyBaseInfo, verifyBaseInfo);
67     APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "call end");
68     return ERR_OK;
69 }
OnConvertToExplicitWant(MessageParcel & data,MessageParcel & reply)70 int32_t AppDomainVerifyAgentServiceStub::OnConvertToExplicitWant(MessageParcel& data, MessageParcel& reply)
71 {
72     APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "%s called", __func__);
73     OHOS::AAFwk::Want want;
74     std::unique_ptr<OHOS::AAFwk::Want> w(data.ReadParcelable<OHOS::AAFwk::Want>());
75     if (!w) {
76         APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "read parcelable want failed.");
77         return ERR_INVALID_VALUE;
78     }
79     want = *w;
80     sptr<IRemoteObject> object = data.ReadRemoteObject();
81     if (object == nullptr) {
82         APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "read failed");
83         return ERR_INVALID_VALUE;
84     }
85     sptr<IConvertCallback> cleanCacheCallback = iface_cast<IConvertCallback>(object);
86     ConvertToExplicitWant(want, cleanCacheCallback);
87     APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "%s call end", __func__);
88     return ERR_OK;
89 }
90 
91 }  // namespace AppDomainVerify
92 }  // namespace OHOS