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     std::vector<CredentialInfo> credInfoList;
154     if (ReadCredentialInfoList(data, credInfoList) != SUCCESS) {
155         IAM_LOGE("ReadCredentialInfoList fail");
156         credInfoList.clear();
157     }
158     OnCredentialInfos(credInfoList);
159     return SUCCESS;
160 }
161 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)162 int32_t IdmGetSecureUserInfoCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
163     MessageOption &option)
164 {
165     IAM_LOGI("code = %{public}u, flags = %{public}d", code, option.GetFlags());
166     if (IdmGetSecureUserInfoCallbackStub::GetDescriptor() != data.ReadInterfaceToken()) {
167         IAM_LOGE("descriptor is not matched");
168         return GENERAL_ERROR;
169     }
170 
171     if (code == IdmGetSecureUserInfoCallbackInterfaceCode::ON_GET_SEC_INFO) {
172         return OnSecureUserInfoStub(data, reply);
173     }
174     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
175 }
176 
ReadSecureUserInfo(MessageParcel & data,SecUserInfo & secUserInfo)177 ResultCode IdmGetSecureUserInfoCallbackStub::ReadSecureUserInfo(MessageParcel &data, SecUserInfo &secUserInfo)
178 {
179     IAM_LOGI("start");
180     uint32_t enrolledInfoLen;
181     if (!data.ReadUint64(secUserInfo.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     IAM_LOGI("read enrolled info vector len: %{public}u", enrolledInfoLen);
190     if (enrolledInfoLen > INFO_VECTOR_LENGTH_LIMIT) {
191         IAM_LOGE("the enrolled info vector size exceed limit");
192         return GENERAL_ERROR;
193     }
194     secUserInfo.enrolledInfo.resize(enrolledInfoLen);
195     for (uint32_t i = 0; i < enrolledInfoLen; ++i) {
196         int32_t authType;
197         uint64_t enrolledId;
198         if (!data.ReadInt32(authType)) {
199             IAM_LOGE("failed to read authType");
200             return READ_PARCEL_ERROR;
201         }
202         if (!data.ReadUint64(enrolledId)) {
203             IAM_LOGE("failed to read enrolledId");
204             return READ_PARCEL_ERROR;
205         }
206         secUserInfo.enrolledInfo[i] = {static_cast<AuthType>(authType), enrolledId};
207     }
208     return SUCCESS;
209 }
210 
OnSecureUserInfoStub(MessageParcel & data,MessageParcel & reply)211 int32_t IdmGetSecureUserInfoCallbackStub::OnSecureUserInfoStub(MessageParcel &data, MessageParcel &reply)
212 {
213     IAM_LOGI("start");
214     SecUserInfo secUserInfo = {};
215 
216     if (ReadSecureUserInfo(data, secUserInfo) != SUCCESS) {
217         IAM_LOGE("ReadSecureUserInfo fail");
218         secUserInfo.secureUid = 0;
219         secUserInfo.enrolledInfo.clear();
220     }
221 
222     OnSecureUserInfo(secUserInfo);
223     return SUCCESS;
224 }
225 } // namespace UserAuth
226 } // namespace UserIam
227 } // namespace OHOS