• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "domain_account_plugin_stub.h"
17 
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 #include "ipc_skeleton.h"
21 
22 namespace OHOS {
23 namespace AccountSA {
24 namespace {
25 const size_t MAX_PASSWORD_SIZE = 4096;
26 }
27 
DomainAccountPluginStub()28 DomainAccountPluginStub::DomainAccountPluginStub()
29 {}
30 
~DomainAccountPluginStub()31 DomainAccountPluginStub::~DomainAccountPluginStub()
32 {}
33 
34 const std::map<std::uint32_t, DomainAccountPluginStub::MessageProcFunction> DomainAccountPluginStub::messageProcMap_ = {
35     {
36         IDomainAccountPlugin::Message::DOMAIN_PLUGIN_AUTH,
37         &DomainAccountPluginStub::ProcAuth
38     },
39     {
40         IDomainAccountPlugin::Message::DOMAIN_PLUGIN_GET_AUTH_PROPERTY,
41         &DomainAccountPluginStub::ProcGetAuthProperty
42     }
43 };
44 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)45 int DomainAccountPluginStub::OnRemoteRequest(
46     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
47 {
48     ACCOUNT_LOGD("Received stub message: %{public}d, callingUid: %{public}d", code, IPCSkeleton::GetCallingUid());
49     if (data.ReadInterfaceToken() != GetDescriptor()) {
50         ACCOUNT_LOGE("check descriptor failed! code %{public}u.", code);
51         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
52     }
53     const auto &itFunc = messageProcMap_.find(code);
54     if (itFunc != messageProcMap_.end()) {
55         return (this->*(itFunc->second))(data, reply);
56     }
57     ACCOUNT_LOGW("remote request unhandled: %{public}d", code);
58     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
59 }
60 
ProcAuth(MessageParcel & data,MessageParcel & reply)61 ErrCode DomainAccountPluginStub::ProcAuth(MessageParcel &data, MessageParcel &reply)
62 {
63     DomainAccountInfo info;
64     if (!data.ReadString(info.accountName_)) {
65         ACCOUNT_LOGE("failed to read domain account name");
66         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
67     }
68     if (!data.ReadString(info.domain_)) {
69         ACCOUNT_LOGE("failed to read domain");
70         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
71     }
72     std::vector<uint8_t> password;
73     if (!data.ReadUInt8Vector(&password)) {
74         ACCOUNT_LOGE("failed to read password");
75         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
76     }
77     sptr<IDomainAuthCallback> callbackProxy = iface_cast<IDomainAuthCallback>(data.ReadRemoteObject());
78     size_t passwordSize = password.size();
79     ErrCode result = ERR_ACCOUNT_COMMON_INVALID_PARAMTER;
80     if (passwordSize > MAX_PASSWORD_SIZE) {
81         ACCOUNT_LOGE("password is too large");
82     } else if (callbackProxy == nullptr) {
83         ACCOUNT_LOGE("invalid callback");
84     } else {
85         result = Auth(info, password, callbackProxy);
86     }
87     for (size_t i = 0; i < passwordSize; ++i) {
88         password[i] = 0;
89     }
90     if (!reply.WriteInt32(result)) {
91         ACCOUNT_LOGE("failed to write result");
92         return IPC_STUB_WRITE_PARCEL_ERR;
93     }
94     return ERR_NONE;
95 }
96 
97 
ProcGetAuthProperty(MessageParcel & data,MessageParcel & reply)98 ErrCode DomainAccountPluginStub::ProcGetAuthProperty(MessageParcel &data, MessageParcel &reply)
99 {
100     DomainAccountInfo info;
101     if (!data.ReadString(info.accountName_)) {
102         ACCOUNT_LOGE("failed to read domain account name");
103         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
104     }
105     if (!data.ReadString(info.domain_)) {
106         ACCOUNT_LOGE("failed to read domain");
107         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
108     }
109     DomainAuthProperty prop;
110     ErrCode result = GetAuthProperty(info, prop);
111     if (!reply.WriteInt32(result)) {
112         ACCOUNT_LOGE("failed to write result");
113         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
114     }
115     if (!reply.WriteInt32(prop.remainingTimes)) {
116         ACCOUNT_LOGE("failed to write remaining times");
117         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
118     }
119     if (!reply.WriteInt32(prop.freezingTime)) {
120         ACCOUNT_LOGE("failed to write freezing time");
121         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
122     }
123     return ERR_NONE;
124 }
125 }  // namespace AccountSA
126 }  // namespace OHOS