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 "co_auth_proxy.h"
17 
18 #include <cinttypes>
19 
20 #include "iam_common_defines.h"
21 #include "iam_logger.h"
22 #include "user_auth_common_defines.h"
23 
24 #define LOG_TAG "AUTH_EXECUTOR_MGR_SDK"
25 
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
CoAuthProxy(const sptr<IRemoteObject> & impl)29 CoAuthProxy::CoAuthProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<CoAuthInterface>(impl)
30 {
31 }
32 
WriteExecutorInfo(const ExecutorRegisterInfo & info,MessageParcel & data)33 int32_t CoAuthProxy::WriteExecutorInfo(const ExecutorRegisterInfo &info, MessageParcel &data)
34 {
35     if (!data.WriteInt32(info.authType)) {
36         IAM_LOGE("failed to write authType");
37         return WRITE_PARCEL_ERROR;
38     }
39     if (!data.WriteInt32(info.executorRole)) {
40         IAM_LOGE("failed to write executorRole");
41         return WRITE_PARCEL_ERROR;
42     }
43     if (!data.WriteUint32(info.executorSensorHint)) {
44         IAM_LOGE("failed to write executorSensorHint");
45         return WRITE_PARCEL_ERROR;
46     }
47     if (!data.WriteUint32(info.executorMatcher)) {
48         IAM_LOGE("failed to write executorMatcher");
49         return WRITE_PARCEL_ERROR;
50     }
51     if (!data.WriteUint32(info.esl)) {
52         IAM_LOGE("failed to write esl");
53         return WRITE_PARCEL_ERROR;
54     }
55     if (!data.WriteUint32(info.maxTemplateAcl)) {
56         IAM_LOGE("failed to write esl");
57         return WRITE_PARCEL_ERROR;
58     }
59     if (!data.WriteUInt8Vector(info.publicKey)) {
60         IAM_LOGE("failed to write publicKey");
61         return WRITE_PARCEL_ERROR;
62     }
63     if (!data.WriteString(info.deviceUdid)) {
64         IAM_LOGE("failed to write deviceUdid");
65         return WRITE_PARCEL_ERROR;
66     }
67     if (!data.WriteUInt8Vector(info.signedRemoteExecutorInfo)) {
68         IAM_LOGE("failed to write signedRemoteExecutorInfo");
69         return WRITE_PARCEL_ERROR;
70     }
71     return SUCCESS;
72 }
73 
ExecutorRegister(const ExecutorRegisterInfo & info,sptr<ExecutorCallbackInterface> & callback)74 uint64_t CoAuthProxy::ExecutorRegister(const ExecutorRegisterInfo &info, sptr<ExecutorCallbackInterface> &callback)
75 {
76     IAM_LOGI("start");
77     if (callback == nullptr) {
78         IAM_LOGE("callback is nullptr");
79         return BAD_CONTEXT_ID;
80     }
81     MessageParcel data;
82     MessageParcel reply;
83 
84     if (!data.WriteInterfaceToken(CoAuthProxy::GetDescriptor())) {
85         IAM_LOGE("failed to write descriptor");
86         return BAD_CONTEXT_ID;
87     }
88     if (WriteExecutorInfo(info, data) != SUCCESS) {
89         IAM_LOGE("failed to write executor info");
90         return BAD_CONTEXT_ID;
91     }
92     if (!data.WriteRemoteObject(callback->AsObject())) {
93         IAM_LOGE("failed to write callback");
94         return BAD_CONTEXT_ID;
95     }
96 
97     bool ret = SendRequest(CoAuthInterfaceCode::CO_AUTH_EXECUTOR_REGISTER, data, reply);
98     if (!ret) {
99         IAM_LOGE("failed to send request");
100         return BAD_CONTEXT_ID;
101     }
102     uint64_t result = 0;
103     if (!reply.ReadUint64(result)) {
104         IAM_LOGE("failed to read result");
105         return BAD_CONTEXT_ID;
106     }
107     return result;
108 }
109 
ExecutorUnregister(uint64_t executorIndex)110 void CoAuthProxy::ExecutorUnregister(uint64_t executorIndex)
111 {
112     IAM_LOGI("start");
113     MessageParcel data;
114     MessageParcel reply;
115 
116     if (!data.WriteInterfaceToken(CoAuthProxy::GetDescriptor())) {
117         IAM_LOGE("failed to write descriptor");
118         return;
119     }
120 
121     if (!data.WriteUint64(executorIndex)) {
122         IAM_LOGE("failed to write executorIndex");
123         return;
124     }
125 
126     bool ret = SendRequest(CoAuthInterfaceCode::CO_AUTH_EXECUTOR_UNREGISTER, data, reply);
127     if (!ret) {
128         IAM_LOGE("failed to send request");
129         return;
130     }
131 }
132 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)133 bool CoAuthProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
134 {
135     IAM_LOGI("code = %{public}u", code);
136     sptr<IRemoteObject> remote = Remote();
137     if (remote == nullptr) {
138         IAM_LOGE("failed to get remote");
139         return false;
140     }
141     MessageOption option(MessageOption::TF_SYNC);
142     int32_t result = remote->SendRequest(code, data, reply, option);
143     if (result != OHOS::NO_ERROR) {
144         IAM_LOGE("failed to send request, result = %{public}d", result);
145         return false;
146     }
147     return true;
148 }
149 } // namespace UserAuth
150 } // namespace UserIam
151 } // namespace OHOS