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_stub.h"
17 
18 #include <cinttypes>
19 
20 #include "executor_callback_proxy.h"
21 #include "iam_logger.h"
22 #include "iam_common_defines.h"
23 #include "string_ex.h"
24 
25 #define LOG_TAG "USER_AUTH_SA"
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
30 
31 // When true is passed into IRemoteStub, sa will process request serially.
CoAuthStub()32 CoAuthStub::CoAuthStub() : IRemoteStub(true) {};
33 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int32_t CoAuthStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
35 {
36     IAM_LOGD("CoAuthStub::OnRemoteRequest, cmd = %{public}u, flags = %{public}d", code, option.GetFlags());
37     if (CoAuthStub::GetDescriptor() != data.ReadInterfaceToken()) {
38         IAM_LOGE("descriptor is not matched");
39         return GENERAL_ERROR;
40     }
41     switch (code) {
42         case CoAuthInterfaceCode::CO_AUTH_EXECUTOR_REGISTER:
43             return ExecutorRegisterStub(data, reply);
44         case CoAuthInterfaceCode::CO_AUTH_EXECUTOR_UNREGISTER:
45             return ExecutorUnregisterStub(data, reply);
46         default:
47             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
48     }
49 }
50 
ExecutorUnregisterStub(MessageParcel & data,MessageParcel & reply)51 int32_t CoAuthStub::ExecutorUnregisterStub(MessageParcel &data, MessageParcel &reply)
52 {
53     uint64_t executorIndex;
54     if (!data.ReadUint64(executorIndex)) {
55         IAM_LOGE("failed to read executorIndex");
56         return GENERAL_ERROR;
57     }
58 
59     ExecutorUnregister(executorIndex);
60     return SUCCESS;
61 }
62 
ExecutorRegisterStub(MessageParcel & data,MessageParcel & reply)63 int32_t CoAuthStub::ExecutorRegisterStub(MessageParcel &data, MessageParcel &reply)
64 {
65     ExecutorRegisterInfo executorInfo = {};
66     if (ReadExecutorRegisterInfo(executorInfo, data) != SUCCESS) {
67         IAM_LOGE("read executorInfo failed");
68         return READ_PARCEL_ERROR;
69     }
70     sptr<IRemoteObject> obj = data.ReadRemoteObject();
71     if (obj == nullptr) {
72         IAM_LOGE("read remote object failed");
73         return READ_PARCEL_ERROR;
74     }
75     sptr<ExecutorCallbackInterface> callback(new (std::nothrow) ExecutorCallbackProxy(obj));
76     if (callback == nullptr) {
77         IAM_LOGE("executor callback is nullptr");
78         return GENERAL_ERROR;
79     }
80 
81     uint64_t executorIndex = ExecutorRegister(executorInfo, callback);
82     if (executorIndex == INVALID_EXECUTOR_INDEX) {
83         IAM_LOGE("executor register failed");
84         return GENERAL_ERROR;
85     }
86     if (!reply.WriteUint64(executorIndex)) {
87         IAM_LOGE("write ExecutorRegister result failed");
88         return WRITE_PARCEL_ERROR;
89     }
90     return SUCCESS;
91 }
92 
ReadExecutorRegisterInfo(ExecutorRegisterInfo & executorInfo,MessageParcel & data)93 int32_t CoAuthStub::ReadExecutorRegisterInfo(ExecutorRegisterInfo &executorInfo, MessageParcel &data)
94 {
95     int32_t authType;
96     int32_t executorRole;
97     int32_t esl;
98 
99     if (!data.ReadInt32(authType)) {
100         IAM_LOGE("failed to read authType");
101         return READ_PARCEL_ERROR;
102     }
103     if (!data.ReadInt32(executorRole)) {
104         IAM_LOGE("failed to read executorRole");
105         return READ_PARCEL_ERROR;
106     }
107     if (!data.ReadUint32(executorInfo.executorSensorHint)) {
108         IAM_LOGE("failed to read executorSensorHint");
109         return READ_PARCEL_ERROR;
110     }
111     if (!data.ReadUint32(executorInfo.executorMatcher)) {
112         IAM_LOGE("failed to read executorMatcher");
113         return READ_PARCEL_ERROR;
114     }
115     if (!data.ReadInt32(esl)) {
116         IAM_LOGE("failed to read esl");
117         return READ_PARCEL_ERROR;
118     }
119     if (!data.ReadUint32(executorInfo.maxTemplateAcl)) {
120         IAM_LOGE("failed to read esl");
121         return READ_PARCEL_ERROR;
122     }
123     if (!data.ReadUInt8Vector(&executorInfo.publicKey)) {
124         IAM_LOGE("failed to read publicKey");
125         return READ_PARCEL_ERROR;
126     }
127     if (!data.ReadString(executorInfo.deviceUdid)) {
128         IAM_LOGE("failed to read publicKey");
129         return READ_PARCEL_ERROR;
130     }
131     if (!data.ReadUInt8Vector(&executorInfo.signedRemoteExecutorInfo)) {
132         IAM_LOGE("failed to read publicKey");
133         return READ_PARCEL_ERROR;
134     }
135 
136     executorInfo.authType = static_cast<AuthType>(authType);
137     executorInfo.executorRole = static_cast<ExecutorRole>(executorRole);
138     executorInfo.esl = static_cast<ExecutorSecureLevel>(esl);
139     return SUCCESS;
140 }
141 } // namespace UserAuth
142 } // namespace UserIam
143 } // namespace OHOS