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 "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_LABEL UserIam::Common::LABEL_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(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(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");
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_SYNC);
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(const std::vector<std::shared_ptr<CredentialInfo>> infoList,const std::optional<PinSubType> pinSubType)104 void IdmGetCredentialInfoProxy::OnCredentialInfos(const std::vector<std::shared_ptr<CredentialInfo>> infoList,
105 const std::optional<PinSubType> pinSubType)
106 {
107 IAM_LOGI("start");
108
109 MessageParcel data;
110 MessageParcel reply;
111
112 if (!data.WriteInterfaceToken(IdmGetCredentialInfoProxy::GetDescriptor())) {
113 IAM_LOGE("failed to write descriptor");
114 return;
115 }
116 if (!data.WriteUint32(infoList.size())) {
117 IAM_LOGE("failed to write infoList.size()");
118 return;
119 }
120 for (const auto &info : infoList) {
121 if (info == nullptr) {
122 return;
123 }
124
125 if (!data.WriteUint64(info->GetCredentialId())) {
126 IAM_LOGE("failed to write credentialId");
127 return;
128 }
129 if (!data.WriteInt32(info->GetAuthType())) {
130 IAM_LOGE("failed to write authType)");
131 return;
132 }
133 if (!data.WriteInt32(pinSubType.value_or(static_cast<PinSubType>(0)))) {
134 IAM_LOGE("failed to write authSubType");
135 return;
136 }
137 if (!data.WriteUint64(info->GetTemplateId())) {
138 IAM_LOGE("failed to write templateId");
139 return;
140 }
141 }
142
143 bool ret = SendRequest(ON_GET_INFO, data, reply);
144 if (!ret) {
145 IAM_LOGE("failed to send request");
146 }
147 }
148
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)149 bool IdmGetCredentialInfoProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
150 {
151 IAM_LOGI("start");
152 sptr<IRemoteObject> remote = Remote();
153 if (remote == nullptr) {
154 IAM_LOGE("failed to get remote");
155 return false;
156 }
157 MessageOption option(MessageOption::TF_SYNC);
158 int32_t result = remote->SendRequest(code, data, reply, option);
159 if (result != SUCCESS) {
160 IAM_LOGE("failed to send request result = %{public}d", result);
161 return false;
162 }
163
164 IAM_LOGI("end");
165 return true;
166 }
167
OnSecureUserInfo(const std::shared_ptr<SecureUserInfo> info)168 void IdmGetSecureUserInfoProxy::OnSecureUserInfo(const std::shared_ptr<SecureUserInfo> info)
169 {
170 IAM_LOGI("start");
171
172 if (info == nullptr) {
173 IAM_LOGE("info is nullptr");
174 return;
175 }
176
177 MessageParcel data;
178 MessageParcel reply;
179 if (!data.WriteInterfaceToken(IdmGetSecureUserInfoProxy::GetDescriptor())) {
180 IAM_LOGE("failed to write descriptor");
181 return;
182 }
183 if (!data.WriteUint64(info->GetSecUserId())) {
184 IAM_LOGE("failed to write secureUid");
185 return;
186 }
187 if (!data.WriteUint32(info->GetEnrolledInfo().size())) {
188 IAM_LOGE("failed to write enrolledInfoLen");
189 return;
190 }
191
192 bool ret = SendRequest(ON_GET_SEC_INFO, data, reply);
193 if (!ret) {
194 IAM_LOGE("failed to send request");
195 }
196 }
197
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)198 bool IdmGetSecureUserInfoProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
199 {
200 IAM_LOGI("start");
201 sptr<IRemoteObject> remote = Remote();
202 if (remote == nullptr) {
203 IAM_LOGE("failed to get remote");
204 return false;
205 }
206 MessageOption option(MessageOption::TF_SYNC);
207 int32_t result = remote->SendRequest(code, data, reply, option);
208 if (result != SUCCESS) {
209 IAM_LOGE("failed to send request result = %{public}d", result);
210 return false;
211 }
212
213 IAM_LOGI("end");
214 return true;
215 }
216 } // namespace UserAuth
217 } // namespace UserIam
218 } // namespace OHOS
219