1 /*
2  * Copyright (c) 2022-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 "account_iam_callback_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 constexpr uint32_t MAX_VEC_SIZE = 1024;
26 }
27 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)28 int IDMCallbackStub::OnRemoteRequest(
29     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
30 {
31     ACCOUNT_LOGD("Received stub message: %{public}d, callingUid: %{public}d, callingPid: %{public}d",
32         code, IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingRealPid());
33     if (data.ReadInterfaceToken() != GetDescriptor()) {
34         ACCOUNT_LOGE("check IDMCallbackStub descriptor failed! code %{public}u.", code);
35         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
36     }
37     switch (code) {
38         case static_cast<uint32_t>(IDMCallbackInterfaceCode::ON_ACQUIRE_INFO):
39             return ProcOnAcquireInfo(data, reply);
40         case static_cast<uint32_t>(IDMCallbackInterfaceCode::ON_RESULT):
41             return ProcOnResult(data, reply);
42         default:
43             break;
44     }
45     ACCOUNT_LOGW("remote request unhandled: %{public}d", code);
46     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
47 }
48 
ProcOnAcquireInfo(MessageParcel & data,MessageParcel & reply)49 ErrCode IDMCallbackStub::ProcOnAcquireInfo(MessageParcel &data, MessageParcel &reply)
50 {
51     int32_t module;
52     int32_t acquireInfo;
53     std::vector<uint8_t> buffer;
54     if (!data.ReadInt32(module)) {
55         ACCOUNT_LOGE("failed to read module");
56         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
57     }
58     if (!data.ReadInt32(acquireInfo)) {
59         ACCOUNT_LOGE("failed to read acquireInfo");
60         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
61     }
62     if (!data.ReadUInt8Vector(&buffer)) {
63         ACCOUNT_LOGE("failed to read buffer");
64         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
65     }
66     Attributes extraInfo(buffer);
67     OnAcquireInfo(module, acquireInfo, extraInfo);
68     return ERR_OK;
69 }
70 
ProcOnResult(MessageParcel & data,MessageParcel & reply)71 ErrCode IDMCallbackStub::ProcOnResult(MessageParcel &data, MessageParcel &reply)
72 {
73     int32_t result;
74     if (!data.ReadInt32(result)) {
75         ACCOUNT_LOGE("failed to read result for IDMCallback OnResult");
76         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
77     }
78     std::vector<uint8_t> buffer;
79     if (!data.ReadUInt8Vector(&buffer)) {
80         ACCOUNT_LOGE("failed to read result for IDMCallback OnResult");
81         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
82     }
83     Attributes extraInfo(buffer);
84     OnResult(result, extraInfo);
85     return ERR_OK;
86 }
87 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)88 int GetCredInfoCallbackStub::OnRemoteRequest(
89     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
90 {
91     ACCOUNT_LOGD("Received stub message: %{public}d, callingUid: %{public}d, callingPid: %{public}d",
92         code, IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingRealPid());
93     if (data.ReadInterfaceToken() != GetDescriptor()) {
94         ACCOUNT_LOGE("check GetCredInfoCallbackStub descriptor failed! code %{public}u.", code);
95         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
96     }
97     switch (code) {
98         case static_cast<uint32_t>(GetCredInfoCallbackInterfaceCode::ON_CREDENTIAL_INFO):
99             return ProcOnCredentialInfo(data, reply);
100         default:
101             break;
102     }
103     ACCOUNT_LOGW("remote request unhandled: %{public}d", code);
104     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
105 }
106 
ProcOnCredentialInfo(MessageParcel & data,MessageParcel & reply)107 ErrCode GetCredInfoCallbackStub::ProcOnCredentialInfo(MessageParcel &data, MessageParcel &reply)
108 {
109     uint32_t vectorSize = 0;
110     std::vector<CredentialInfo> infoList;
111     if (!data.ReadUint32(vectorSize)) {
112         ACCOUNT_LOGE("read size fail");
113         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
114     }
115     if (vectorSize > MAX_VEC_SIZE) {
116         ACCOUNT_LOGE("credential info list is oversize, the limit is %{public}d", MAX_VEC_SIZE);
117         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
118     }
119     for (uint32_t i = 0; i < vectorSize; ++i) {
120         CredentialInfo info;
121         int32_t authType = 0;
122         int32_t pinType = 0;
123         if (!data.ReadUint64(info.credentialId)) {
124             ACCOUNT_LOGE("failed to read credentialId");
125             return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
126         }
127         if (!data.ReadInt32(authType)) {
128             ACCOUNT_LOGE("failed to read authType");
129             return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
130         }
131         if (!data.ReadInt32(pinType)) {
132             ACCOUNT_LOGE("failed to read pinSubType");
133             return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
134         }
135         if (!data.ReadUint64(info.templateId)) {
136             ACCOUNT_LOGE("failed to read templateId");
137             return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
138         }
139         info.authType = static_cast<AuthType>(authType);
140         info.pinType = static_cast<PinSubType>(pinType);
141         infoList.push_back(info);
142     }
143     OnCredentialInfo(infoList);
144     return ERR_OK;
145 }
146 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)147 int GetSetPropCallbackStub::OnRemoteRequest(
148     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
149 {
150     ACCOUNT_LOGD("Received stub message: %{public}d, callingUid: %{public}d, callingPid: %{public}d",
151         code, IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingRealPid());
152     if (data.ReadInterfaceToken() != GetDescriptor()) {
153         ACCOUNT_LOGE("check GetSetPropCallbackStub descriptor failed! code %{public}u.", code);
154         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
155     }
156     switch (code) {
157         case static_cast<uint32_t>(GetSetPropCallbackInterfaceCode::ON_RESULT):
158             return ProcOnResult(data, reply);
159         default:
160             break;
161     }
162     ACCOUNT_LOGW("remote request unhandled: %{public}d", code);
163     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
164 }
165 
ProcOnResult(MessageParcel & data,MessageParcel & reply)166 ErrCode GetSetPropCallbackStub::ProcOnResult(MessageParcel &data, MessageParcel &reply)
167 {
168     int32_t result;
169     if (!data.ReadInt32(result)) {
170         ACCOUNT_LOGE("failed to read result for GetSetPropCallback OnResult");
171         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
172     }
173     std::vector<uint8_t> buffer;
174     if (!data.ReadUInt8Vector(&buffer)) {
175         ACCOUNT_LOGE("failed to read result for GetSetPropCallback OnResult");
176         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
177     }
178     Attributes extraInfo(buffer);
179     OnResult(result, extraInfo);
180     return ERR_OK;
181 }
182 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)183 int GetEnrolledIdCallbackStub::OnRemoteRequest(
184     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
185 {
186     ACCOUNT_LOGD("Received stub message: %{public}d, callingUid: %{public}d, callingPid: %{public}d",
187         code, IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingRealPid());
188     if (data.ReadInterfaceToken() != GetDescriptor()) {
189         ACCOUNT_LOGE("Check GetCredInfoCallbackStub descriptor failed! code %{public}u.", code);
190         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
191     }
192     switch (code) {
193         case static_cast<uint32_t>(GetEnrolledIdCallbackInterfaceCode::ON_ENROLLED_ID):
194             return ProcOnEnrolledId(data, reply);
195         default:
196             break;
197     }
198     ACCOUNT_LOGW("Remote request unhandled: %{public}d", code);
199     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
200 }
201 
ProcOnEnrolledId(MessageParcel & data,MessageParcel & reply)202 ErrCode GetEnrolledIdCallbackStub::ProcOnEnrolledId(MessageParcel &data, MessageParcel &reply)
203 {
204     int32_t result;
205     if (!data.ReadInt32(result)) {
206         ACCOUNT_LOGE("Failed to read result");
207         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
208     }
209     uint64_t enrolledId;
210     if (!data.ReadUint64(enrolledId)) {
211         ACCOUNT_LOGE("Failed to read enrolledId");
212         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
213     }
214     OnEnrolledId(result, enrolledId);
215     return ERR_OK;
216 }
217 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)218 int PreRemoteAuthCallbackStub::OnRemoteRequest(
219     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
220 {
221     ACCOUNT_LOGD("Received stub message: %{public}d, callingUid: %{public}d, callingPid: %{public}d",
222         code, IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingRealPid());
223     if (data.ReadInterfaceToken() != GetDescriptor()) {
224         ACCOUNT_LOGE("Check PreRemoteAuthCallbackStub descriptor failed, code=%{public}u.", code);
225         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
226     }
227     switch (code) {
228         case static_cast<uint32_t>(PreRemoteAuthCallbackInterfaceCode::ON_RESULT):
229             return ProcOnResult(data, reply);
230         default:
231             break;
232     }
233     ACCOUNT_LOGW("Remote request unhandled: %{public}d.", code);
234     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
235 }
236 
ProcOnResult(MessageParcel & data,MessageParcel & reply)237 ErrCode PreRemoteAuthCallbackStub::ProcOnResult(MessageParcel &data, MessageParcel &reply)
238 {
239     int32_t result;
240     if (!data.ReadInt32(result)) {
241         ACCOUNT_LOGE("Read result for PreRemoteAuthCallbackStub OnResult failed.");
242         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
243     }
244     OnResult(result);
245     return ERR_OK;
246 }
247 }  // namespace AccountSA
248 }  // namespace OHOS
249