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_stub.h"
17
18 #include "iam_logger.h"
19 #include "user_idm_client_defines.h"
20
21 #define LOG_TAG "USER_IDM_SDK"
22
23 namespace OHOS {
24 namespace UserIam {
25 namespace UserAuth {
26 namespace {
27 const uint32_t INFO_VECTOR_LENGTH_LIMIT = 100;
28 } // namespace
29
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)30 int32_t IdmCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
31 MessageOption &option)
32 {
33 IAM_LOGI("code = %{public}u, flags = %{public}d", code, option.GetFlags());
34 if (IdmCallbackStub::GetDescriptor() != data.ReadInterfaceToken()) {
35 IAM_LOGE("descriptor is not matched");
36 return GENERAL_ERROR;
37 }
38
39 switch (code) {
40 case IdmCallbackInterfaceCode::IDM_CALLBACK_ON_RESULT:
41 return OnResultStub(data, reply);
42 case IdmCallbackInterfaceCode::IDM_CALLBACK_ON_ACQUIRE_INFO:
43 return OnAcquireInfoStub(data, reply);
44 default:
45 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
46 }
47 }
48
OnResultStub(MessageParcel & data,MessageParcel & reply)49 int32_t IdmCallbackStub::OnResultStub(MessageParcel &data, MessageParcel &reply)
50 {
51 IAM_LOGI("start");
52 int32_t result;
53 std::vector<uint8_t> buffer;
54
55 if (!data.ReadInt32(result)) {
56 IAM_LOGE("failed to read result");
57 return READ_PARCEL_ERROR;
58 }
59 if (!data.ReadUInt8Vector(&buffer)) {
60 IAM_LOGE("failed to read buffer");
61 return READ_PARCEL_ERROR;
62 }
63
64 Attributes extraInfo(buffer);
65 OnResult(result, extraInfo);
66 return SUCCESS;
67 }
68
OnAcquireInfoStub(MessageParcel & data,MessageParcel & reply)69 int32_t IdmCallbackStub::OnAcquireInfoStub(MessageParcel &data, MessageParcel &reply)
70 {
71 IAM_LOGI("start");
72 int32_t module;
73 int32_t acquireInfo;
74 std::vector<uint8_t> buffer;
75
76 if (!data.ReadInt32(module)) {
77 IAM_LOGE("failed to read module");
78 return READ_PARCEL_ERROR;
79 }
80 if (!data.ReadInt32(acquireInfo)) {
81 IAM_LOGE("failed to read acquireInfo");
82 return READ_PARCEL_ERROR;
83 }
84 if (!data.ReadUInt8Vector(&buffer)) {
85 IAM_LOGE("failed to read buffer");
86 return READ_PARCEL_ERROR;
87 }
88
89 Attributes extraInfo(buffer);
90 OnAcquireInfo(module, acquireInfo, extraInfo);
91 return SUCCESS;
92 }
93
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)94 int32_t IdmGetCredInfoCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
95 MessageOption &option)
96 {
97 IAM_LOGI("code = %{public}u, flags = %{public}d", code, option.GetFlags());
98 if (IdmGetCredInfoCallbackStub::GetDescriptor() != data.ReadInterfaceToken()) {
99 IAM_LOGE("descriptor is not matched");
100 return GENERAL_ERROR;
101 }
102
103 if (code == IdmGetCredInfoCallbackInterfaceCode::ON_GET_INFO) {
104 return OnCredentialInfosStub(data, reply);
105 }
106 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
107 }
108
ReadCredentialInfoList(MessageParcel & data,std::vector<CredentialInfo> & credInfoList)109 ResultCode IdmGetCredInfoCallbackStub::ReadCredentialInfoList(MessageParcel &data,
110 std::vector<CredentialInfo> &credInfoList)
111 {
112 IAM_LOGI("start");
113 uint32_t credInfosLen = 0;
114 if (!data.ReadUint32(credInfosLen)) {
115 IAM_LOGE("read credInfosLen fail");
116 return READ_PARCEL_ERROR;
117 }
118 IAM_LOGI("read cred info vector len: %{public}u", credInfosLen);
119 if (credInfosLen > INFO_VECTOR_LENGTH_LIMIT) {
120 IAM_LOGE("the cred info vector size exceed limit");
121 return GENERAL_ERROR;
122 }
123 for (uint32_t i = 0; i < credInfosLen; ++i) {
124 CredentialInfo info = {};
125 int32_t authType;
126 int32_t pinType = 0;
127 if (!data.ReadUint64(info.credentialId)) {
128 IAM_LOGE("failed to read credentialId");
129 return READ_PARCEL_ERROR;
130 }
131 if (!data.ReadInt32(authType)) {
132 IAM_LOGE("failed to read authType");
133 return READ_PARCEL_ERROR;
134 }
135 if (!data.ReadInt32(pinType)) {
136 IAM_LOGE("failed to read pinSubType");
137 return READ_PARCEL_ERROR;
138 }
139 if (!data.ReadUint64(info.templateId)) {
140 IAM_LOGE("failed to read templateId");
141 return READ_PARCEL_ERROR;
142 }
143 info.authType = static_cast<AuthType>(authType);
144 info.pinType = static_cast<PinSubType>(pinType);
145 credInfoList.push_back(info);
146 }
147 return SUCCESS;
148 }
149
OnCredentialInfosStub(MessageParcel & data,MessageParcel & reply)150 int32_t IdmGetCredInfoCallbackStub::OnCredentialInfosStub(MessageParcel &data, MessageParcel &reply)
151 {
152 IAM_LOGI("start");
153 int32_t result = GENERAL_ERROR;
154 if (!data.ReadInt32(result)) {
155 IAM_LOGE("failed to read result");
156 return READ_PARCEL_ERROR;
157 }
158 std::vector<CredentialInfo> credInfoList;
159 if (ReadCredentialInfoList(data, credInfoList) != SUCCESS) {
160 IAM_LOGE("ReadCredentialInfoList fail");
161 credInfoList.clear();
162 }
163 OnCredentialInfos(result, credInfoList);
164 return SUCCESS;
165 }
166
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)167 int32_t IdmGetSecureUserInfoCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
168 MessageOption &option)
169 {
170 IAM_LOGI("code = %{public}u, flags = %{public}d", code, option.GetFlags());
171 if (IdmGetSecureUserInfoCallbackStub::GetDescriptor() != data.ReadInterfaceToken()) {
172 IAM_LOGE("descriptor is not matched");
173 return GENERAL_ERROR;
174 }
175
176 if (code == IdmGetSecureUserInfoCallbackInterfaceCode::ON_GET_SEC_INFO) {
177 return OnSecureUserInfoStub(data, reply);
178 }
179 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
180 }
181
ReadSecureUserInfo(MessageParcel & data,SecUserInfo & secUserInfo)182 ResultCode IdmGetSecureUserInfoCallbackStub::ReadSecureUserInfo(MessageParcel &data, SecUserInfo &secUserInfo)
183 {
184 IAM_LOGI("start");
185 uint32_t enrolledInfoLen;
186 if (!data.ReadUint64(secUserInfo.secureUid)) {
187 IAM_LOGE("failed to read secureUid");
188 return READ_PARCEL_ERROR;
189 }
190 if (!data.ReadUint32(enrolledInfoLen)) {
191 IAM_LOGE("failed to read enrolledInfoLen");
192 return READ_PARCEL_ERROR;
193 }
194 IAM_LOGI("read enrolled info vector len: %{public}u", enrolledInfoLen);
195 if (enrolledInfoLen > INFO_VECTOR_LENGTH_LIMIT) {
196 IAM_LOGE("the enrolled info vector size exceed limit");
197 return GENERAL_ERROR;
198 }
199 secUserInfo.enrolledInfo.resize(enrolledInfoLen);
200 for (uint32_t i = 0; i < enrolledInfoLen; ++i) {
201 int32_t authType;
202 uint64_t enrolledId;
203 if (!data.ReadInt32(authType)) {
204 IAM_LOGE("failed to read authType");
205 return READ_PARCEL_ERROR;
206 }
207 if (!data.ReadUint64(enrolledId)) {
208 IAM_LOGE("failed to read enrolledId");
209 return READ_PARCEL_ERROR;
210 }
211 secUserInfo.enrolledInfo[i] = {static_cast<AuthType>(authType), enrolledId};
212 }
213 return SUCCESS;
214 }
215
OnSecureUserInfoStub(MessageParcel & data,MessageParcel & reply)216 int32_t IdmGetSecureUserInfoCallbackStub::OnSecureUserInfoStub(MessageParcel &data, MessageParcel &reply)
217 {
218 IAM_LOGI("start");
219 int32_t result = GENERAL_ERROR;
220 if (!data.ReadInt32(result)) {
221 IAM_LOGE("failed to read result");
222 return READ_PARCEL_ERROR;
223 }
224 SecUserInfo secUserInfo = {};
225 if (ReadSecureUserInfo(data, secUserInfo) != SUCCESS) {
226 IAM_LOGE("ReadSecureUserInfo fail");
227 secUserInfo.secureUid = 0;
228 secUserInfo.enrolledInfo.clear();
229 }
230
231 OnSecureUserInfo(result, secUserInfo);
232 return SUCCESS;
233 }
234 } // namespace UserAuth
235 } // namespace UserIam
236 } // namespace OHOS