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_stub.h"
17
18 #include "iam_logger.h"
19 #include "iam_ptr.h"
20 #include "sec_user_info_impl.h"
21 #include "user_idm_client_defines.h"
22 #include "cred_info_impl.h"
23
24 #define LOG_LABEL UserIam::Common::LABEL_USER_IDM_SDK
25
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
29 namespace {
30 const uint32_t CRED_INFO_VECTOR_LENGTH_LIMIT = 100;
31 } // namespace
32
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t IdmCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
34 MessageOption &option)
35 {
36 IAM_LOGD("cmd = %{public}u, flags = %{public}d", code, option.GetFlags());
37 if (IdmCallbackStub::GetDescriptor() != data.ReadInterfaceToken()) {
38 IAM_LOGE("descriptor is not matched");
39 return GENERAL_ERROR;
40 }
41
42 switch (code) {
43 case IdmCallbackInterface::IDM_CALLBACK_ON_RESULT:
44 return OnResultStub(data, reply);
45 case IdmCallbackInterface::IDM_CALLBACK_ON_ACQUIRE_INFO:
46 return OnAcquireInfoStub(data, reply);
47 default:
48 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
49 }
50 }
51
OnResultStub(MessageParcel & data,MessageParcel & reply)52 int32_t IdmCallbackStub::OnResultStub(MessageParcel &data, MessageParcel &reply)
53 {
54 int32_t result;
55 std::vector<uint8_t> buffer;
56
57 if (!data.ReadInt32(result)) {
58 IAM_LOGE("failed to read result");
59 return READ_PARCEL_ERROR;
60 }
61 if (!data.ReadUInt8Vector(&buffer)) {
62 IAM_LOGE("failed to read buffer");
63 return READ_PARCEL_ERROR;
64 }
65
66 Attributes extraInfo(buffer);
67 OnResult(result, extraInfo);
68 return SUCCESS;
69 }
70
OnAcquireInfoStub(MessageParcel & data,MessageParcel & reply)71 int32_t IdmCallbackStub::OnAcquireInfoStub(MessageParcel &data, MessageParcel &reply)
72 {
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_LOGD("cmd = %{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 == IdmGetCredInfoCallbackInterface::ON_GET_INFO) {
105 return OnCredentialInfosStub(data, reply);
106 }
107 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
108 }
109
OnCredentialInfosStub(MessageParcel & data,MessageParcel & reply)110 int32_t IdmGetCredInfoCallbackStub::OnCredentialInfosStub(MessageParcel &data, MessageParcel &reply)
111 {
112 uint32_t vectorSize = 0;
113 std::vector<std::shared_ptr<IdmGetCredInfoCallbackInterface::CredentialInfo>> infoList;
114 if (!data.ReadUint32(vectorSize)) {
115 IAM_LOGE("read size fail");
116 OnCredentialInfos(infoList, std::nullopt);
117 return READ_PARCEL_ERROR;
118 }
119 if (vectorSize > CRED_INFO_VECTOR_LENGTH_LIMIT) {
120 IAM_LOGI("the cred info vector size is invalid");
121 return GENERAL_ERROR;
122 }
123 int32_t pinType = 0;
124 for (uint32_t i = 0; i < vectorSize; ++i) {
125 uint64_t credentialId;
126 uint64_t templateId;
127 int32_t authType;
128 if (!data.ReadUint64(credentialId)) {
129 IAM_LOGE("failed to read credentialId");
130 OnCredentialInfos(infoList, std::nullopt);
131 return READ_PARCEL_ERROR;
132 }
133 if (!data.ReadInt32(authType)) {
134 IAM_LOGE("failed to read authType");
135 OnCredentialInfos(infoList, std::nullopt);
136 return READ_PARCEL_ERROR;
137 }
138 if (!data.ReadInt32(pinType)) {
139 IAM_LOGE("failed to read pinSubType");
140 OnCredentialInfos(infoList, std::nullopt);
141 return READ_PARCEL_ERROR;
142 }
143 if (!data.ReadUint64(templateId)) {
144 IAM_LOGE("failed to read templateId");
145 OnCredentialInfos(infoList, std::nullopt);
146 return READ_PARCEL_ERROR;
147 }
148 auto credInfo = Common::MakeShared<CredInfoImpl>(credentialId, templateId,
149 static_cast<AuthType>(authType));
150 infoList.push_back(credInfo);
151 }
152
153 std::optional<PinSubType> pinSubType = std::nullopt;
154 if (pinType != 0) {
155 pinSubType = static_cast<PinSubType>(pinType);
156 }
157 OnCredentialInfos(infoList, pinSubType);
158 return SUCCESS;
159 }
160
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)161 int32_t IdmGetSecureUserInfoCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
162 MessageOption &option)
163 {
164 IAM_LOGD("cmd = %{public}u, flags = %{public}d", code, option.GetFlags());
165 if (IdmGetSecureUserInfoCallbackStub::GetDescriptor() != data.ReadInterfaceToken()) {
166 IAM_LOGE("descriptor is not matched");
167 return GENERAL_ERROR;
168 }
169
170 if (code == IdmGetSecureUserInfoCallbackInterface::ON_GET_SEC_INFO) {
171 return OnSecureUserInfoStub(data, reply);
172 }
173 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
174 }
175
OnSecureUserInfoStub(MessageParcel & data,MessageParcel & reply)176 int32_t IdmGetSecureUserInfoCallbackStub::OnSecureUserInfoStub(MessageParcel &data, MessageParcel &reply)
177 {
178 uint64_t secureUid;
179 uint32_t enrolledInfoLen;
180
181 if (!data.ReadUint64(secureUid)) {
182 IAM_LOGE("failed to read secureUid");
183 return READ_PARCEL_ERROR;
184 }
185 if (!data.ReadUint32(enrolledInfoLen)) {
186 IAM_LOGE("failed to read enrolledInfoLen");
187 return READ_PARCEL_ERROR;
188 }
189
190 std::vector<std::shared_ptr<SecEnrolledInfo>> info;
191 auto secUserInfo = Common::MakeShared<SecUserInfoImpl>(secureUid, info);
192 OnSecureUserInfo(secUserInfo);
193 return SUCCESS;
194 }
195 } // namespace UserAuth
196 } // namespace UserIam
197 } // namespace OHOS