1 /*
2 * Copyright (c) 2022-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 "user_idm_callback_proxy.h"
17
18 #include "iam_logger.h"
19 #include "iam_common_defines.h"
20 #include "user_idm_interface.h"
21
22 #define LOG_TAG "USER_AUTH_SA"
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
OnResult(int32_t result,const Attributes & extraInfo)27 void IdmCallbackProxy::OnResult(int32_t result, const Attributes &extraInfo)
28 {
29 IAM_LOGI("start");
30
31 MessageParcel data;
32 MessageParcel reply;
33
34 if (!data.WriteInterfaceToken(IdmCallbackProxy::GetDescriptor())) {
35 IAM_LOGE("failed to write descriptor");
36 return;
37 }
38 if (!data.WriteInt32(result)) {
39 IAM_LOGE("failed to write result");
40 return;
41 }
42 auto buffer = extraInfo.Serialize();
43 if (!data.WriteUInt8Vector(buffer)) {
44 IAM_LOGE("failed to write buffer");
45 return;
46 }
47
48 bool ret = SendRequest(IdmCallbackInterfaceCode::IDM_CALLBACK_ON_RESULT, data, reply);
49 if (!ret) {
50 IAM_LOGE("failed to send request");
51 }
52 }
53
OnAcquireInfo(int32_t module,int32_t acquireInfo,const Attributes & extraInfo)54 void IdmCallbackProxy::OnAcquireInfo(int32_t module, int32_t acquireInfo, const Attributes &extraInfo)
55 {
56 IAM_LOGI("start");
57
58 MessageParcel data;
59 MessageParcel reply;
60
61 if (!data.WriteInterfaceToken(IdmCallbackProxy::GetDescriptor())) {
62 IAM_LOGE("failed to write descriptor");
63 return;
64 }
65 if (!data.WriteInt32(module)) {
66 IAM_LOGE("failed to write module");
67 return;
68 }
69 if (!data.WriteInt32(acquireInfo)) {
70 IAM_LOGE("failed to write acquire");
71 return;
72 }
73 auto buffer = extraInfo.Serialize();
74 if (!data.WriteUInt8Vector(buffer)) {
75 IAM_LOGE("failed to write buffer");
76 return;
77 }
78
79 bool ret = SendRequest(IdmCallbackInterfaceCode::IDM_CALLBACK_ON_ACQUIRE_INFO, data, reply);
80 if (!ret) {
81 IAM_LOGE("failed to send request");
82 }
83 }
84
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)85 bool IdmCallbackProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
86 {
87 IAM_LOGI("start code = %{public}u", code);
88 sptr<IRemoteObject> remote = Remote();
89 if (remote == nullptr) {
90 IAM_LOGE("failed to get remote");
91 return false;
92 }
93 MessageOption option(MessageOption::TF_ASYNC);
94 int32_t result = remote->SendRequest(code, data, reply, option);
95 if (result != SUCCESS) {
96 IAM_LOGE("failed to send request, result = %{public}d", result);
97 return false;
98 }
99
100 IAM_LOGI("end");
101 return true;
102 }
103
OnCredentialInfos(int32_t result,const std::vector<CredentialInfo> & credInfoList)104 void IdmGetCredentialInfoProxy::OnCredentialInfos(int32_t result, const std::vector<CredentialInfo> &credInfoList)
105 {
106 IAM_LOGI("start, cred info vector size: %{public}zu", credInfoList.size());
107
108 MessageParcel data;
109 MessageParcel reply;
110
111 if (!data.WriteInterfaceToken(IdmGetCredentialInfoProxy::GetDescriptor())) {
112 IAM_LOGE("failed to write descriptor");
113 return;
114 }
115 if (!data.WriteInt32(result)) {
116 IAM_LOGE("failed to write result");
117 return;
118 }
119 if (!data.WriteUint32(credInfoList.size())) {
120 IAM_LOGE("failed to write credInfoList size");
121 return;
122 }
123 for (const auto &info : credInfoList) {
124 if (!data.WriteUint64(info.credentialId)) {
125 IAM_LOGE("failed to write credentialId");
126 return;
127 }
128 if (!data.WriteInt32(info.authType)) {
129 IAM_LOGE("failed to write authType");
130 return;
131 }
132 if (!data.WriteInt32(info.pinType.value_or(static_cast<PinSubType>(0)))) {
133 IAM_LOGE("failed to write pinSubType");
134 return;
135 }
136 if (!data.WriteUint64(info.templateId)) {
137 IAM_LOGE("failed to write templateId");
138 return;
139 }
140 }
141
142 bool ret = SendRequest(IdmGetCredInfoCallbackInterfaceCode::ON_GET_INFO, data, reply);
143 if (!ret) {
144 IAM_LOGE("failed to send request");
145 }
146 }
147
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)148 bool IdmGetCredentialInfoProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
149 {
150 IAM_LOGI("start code = %{public}u", code);
151 sptr<IRemoteObject> remote = Remote();
152 if (remote == nullptr) {
153 IAM_LOGE("failed to get remote");
154 return false;
155 }
156 MessageOption option(MessageOption::TF_ASYNC);
157 int32_t result = remote->SendRequest(code, data, reply, option);
158 if (result != SUCCESS) {
159 IAM_LOGE("failed to send request result = %{public}d", result);
160 return false;
161 }
162
163 IAM_LOGI("end");
164 return true;
165 }
166
WriteSecureUserInfo(MessageParcel & data,const SecUserInfo & secUserInfo)167 ResultCode IdmGetSecureUserInfoProxy::WriteSecureUserInfo(MessageParcel &data, const SecUserInfo &secUserInfo)
168
169 {
170 IAM_LOGI("start");
171 if (!data.WriteUint64(secUserInfo.secureUid)) {
172 IAM_LOGE("failed to write secureUid");
173 return WRITE_PARCEL_ERROR;
174 }
175 uint32_t enrolledInfoLen = secUserInfo.enrolledInfo.size();
176 IAM_LOGI("write enrolled info vector len: %{public}u", enrolledInfoLen);
177 if (!data.WriteUint32(enrolledInfoLen)) {
178 IAM_LOGE("failed to write enrolledInfoLen");
179 return WRITE_PARCEL_ERROR;
180 }
181 for (const auto &info : secUserInfo.enrolledInfo) {
182 if (!data.WriteInt32(info.authType)) {
183 IAM_LOGE("failed to write authType");
184 return WRITE_PARCEL_ERROR;
185 }
186 if (!data.WriteUint64(info.enrolledId)) {
187 IAM_LOGE("failed to write enrolledId");
188 return WRITE_PARCEL_ERROR;
189 }
190 }
191 return SUCCESS;
192 }
193
OnSecureUserInfo(int32_t result,const SecUserInfo & secUserInfo)194 void IdmGetSecureUserInfoProxy::OnSecureUserInfo(int32_t result, const SecUserInfo &secUserInfo)
195 {
196 IAM_LOGI("start");
197
198 MessageParcel data;
199 MessageParcel reply;
200 if (!data.WriteInterfaceToken(IdmGetSecureUserInfoProxy::GetDescriptor())) {
201 IAM_LOGE("failed to write descriptor");
202 return;
203 }
204 if (!data.WriteInt32(result)) {
205 IAM_LOGE("failed to write result");
206 return;
207 }
208 if (WriteSecureUserInfo(data, secUserInfo) != SUCCESS) {
209 IAM_LOGE("WriteSecureUserInfo fail");
210 return;
211 }
212
213 bool ret = SendRequest(IdmGetSecureUserInfoCallbackInterfaceCode::ON_GET_SEC_INFO, data, reply);
214 if (!ret) {
215 IAM_LOGE("failed to send request");
216 }
217 }
218
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)219 bool IdmGetSecureUserInfoProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
220 {
221 IAM_LOGI("start code = %{public}u", code);
222 sptr<IRemoteObject> remote = Remote();
223 if (remote == nullptr) {
224 IAM_LOGE("failed to get remote");
225 return false;
226 }
227 MessageOption option(MessageOption::TF_ASYNC);
228 int32_t result = remote->SendRequest(code, data, reply, option);
229 if (result != SUCCESS) {
230 IAM_LOGE("failed to send request result = %{public}d", result);
231 return false;
232 }
233
234 IAM_LOGI("end");
235 return true;
236 }
237 } // namespace UserAuth
238 } // namespace UserIam
239 } // namespace OHOS
240