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(const std::vector<CredentialInfo> & credInfoList)104 void IdmGetCredentialInfoProxy::OnCredentialInfos(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.WriteUint32(credInfoList.size())) {
116         IAM_LOGE("failed to write credInfoList size");
117         return;
118     }
119     for (const auto &info : credInfoList) {
120         if (!data.WriteUint64(info.credentialId)) {
121             IAM_LOGE("failed to write credentialId");
122             return;
123         }
124         if (!data.WriteInt32(info.authType)) {
125             IAM_LOGE("failed to write authType");
126             return;
127         }
128         if (!data.WriteInt32(info.pinType.value_or(static_cast<PinSubType>(0)))) {
129             IAM_LOGE("failed to write pinSubType");
130             return;
131         }
132         if (!data.WriteUint64(info.templateId)) {
133             IAM_LOGE("failed to write templateId");
134             return;
135         }
136     }
137 
138     bool ret = SendRequest(IdmGetCredInfoCallbackInterfaceCode::ON_GET_INFO, data, reply);
139     if (!ret) {
140         IAM_LOGE("failed to send request");
141     }
142 }
143 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)144 bool IdmGetCredentialInfoProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
145 {
146     IAM_LOGI("start code = %{public}u", code);
147     sptr<IRemoteObject> remote = Remote();
148     if (remote == nullptr) {
149         IAM_LOGE("failed to get remote");
150         return false;
151     }
152     MessageOption option(MessageOption::TF_ASYNC);
153     int32_t result = remote->SendRequest(code, data, reply, option);
154     if (result != SUCCESS) {
155         IAM_LOGE("failed to send request result = %{public}d", result);
156         return false;
157     }
158 
159     IAM_LOGI("end");
160     return true;
161 }
162 
WriteSecureUserInfo(MessageParcel & data,const SecUserInfo & secUserInfo)163 ResultCode IdmGetSecureUserInfoProxy::WriteSecureUserInfo(MessageParcel &data, const SecUserInfo &secUserInfo)
164 
165 {
166     IAM_LOGI("start");
167     if (!data.WriteInterfaceToken(IdmGetSecureUserInfoProxy::GetDescriptor())) {
168         IAM_LOGE("failed to write descriptor");
169         return WRITE_PARCEL_ERROR;
170     }
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(const SecUserInfo & secUserInfo)194 void IdmGetSecureUserInfoProxy::OnSecureUserInfo(const SecUserInfo &secUserInfo)
195 {
196     IAM_LOGI("start");
197 
198     MessageParcel data;
199     MessageParcel reply;
200     if (WriteSecureUserInfo(data, secUserInfo) != SUCCESS) {
201         IAM_LOGE("WriteSecureUserInfo fail");
202         return;
203     }
204 
205     bool ret = SendRequest(IdmGetSecureUserInfoCallbackInterfaceCode::ON_GET_SEC_INFO, data, reply);
206     if (!ret) {
207         IAM_LOGE("failed to send request");
208     }
209 }
210 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)211 bool IdmGetSecureUserInfoProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
212 {
213     IAM_LOGI("start code = %{public}u", code);
214     sptr<IRemoteObject> remote = Remote();
215     if (remote == nullptr) {
216         IAM_LOGE("failed to get remote");
217         return false;
218     }
219     MessageOption option(MessageOption::TF_ASYNC);
220     int32_t result = remote->SendRequest(code, data, reply, option);
221     if (result != SUCCESS) {
222         IAM_LOGE("failed to send request result = %{public}d", result);
223         return false;
224     }
225 
226     IAM_LOGI("end");
227     return true;
228 }
229 } // namespace UserAuth
230 } // namespace UserIam
231 } // namespace OHOS
232