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