• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LABEL UserIam::Common::LABEL_USER_AUTH_SA
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)30 int32_t CoAuthStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
31 {
32     IAM_LOGD("CoAuthStub::OnRemoteRequest, cmd = %{public}u, flags = %{public}d", code, option.GetFlags());
33     if (CoAuthStub::GetDescriptor() != data.ReadInterfaceToken()) {
34         IAM_LOGE("descriptor is not matched");
35         return GENERAL_ERROR;
36     }
37     switch (code) {
38         case CoAuthInterfaceCode::CO_AUTH_EXECUTOR_REGISTER:
39             return ExecutorRegisterStub(data, reply);
40         default:
41             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
42     }
43 }
44 
ExecutorRegisterStub(MessageParcel & data,MessageParcel & reply)45 int32_t CoAuthStub::ExecutorRegisterStub(MessageParcel &data, MessageParcel &reply)
46 {
47     ExecutorRegisterInfo executorInfo = {};
48     if (ReadExecutorRegisterInfo(executorInfo, data) != SUCCESS) {
49         IAM_LOGE("read executorInfo failed");
50         return READ_PARCEL_ERROR;
51     }
52     sptr<IRemoteObject> obj = data.ReadRemoteObject();
53     if (obj == nullptr) {
54         IAM_LOGE("read remote object failed");
55         return READ_PARCEL_ERROR;
56     }
57     sptr<ExecutorCallbackInterface> callback(new (std::nothrow) ExecutorCallbackProxy(obj));
58     if (callback == nullptr) {
59         IAM_LOGE("executor callback is nullptr");
60         return GENERAL_ERROR;
61     }
62 
63     uint64_t executorIndex = ExecutorRegister(executorInfo, callback);
64     if (executorIndex == INVALID_EXECUTOR_INDEX) {
65         IAM_LOGE("executor register failed");
66         return GENERAL_ERROR;
67     }
68     if (!reply.WriteUint64(executorIndex)) {
69         IAM_LOGE("write ExecutorRegister result failed");
70         return WRITE_PARCEL_ERROR;
71     }
72     return SUCCESS;
73 }
74 
ReadExecutorRegisterInfo(ExecutorRegisterInfo & executorInfo,MessageParcel & data)75 int32_t CoAuthStub::ReadExecutorRegisterInfo(ExecutorRegisterInfo &executorInfo, MessageParcel &data)
76 {
77     int32_t authType;
78     int32_t executorRole;
79     uint32_t executorSensorHint;
80     uint32_t executorMatcher;
81     int32_t esl;
82 
83     if (!data.ReadInt32(authType)) {
84         IAM_LOGE("failed to read authType");
85         return READ_PARCEL_ERROR;
86     }
87     if (!data.ReadInt32(executorRole)) {
88         IAM_LOGE("failed to read executorRole");
89         return READ_PARCEL_ERROR;
90     }
91     if (!data.ReadUint32(executorSensorHint)) {
92         IAM_LOGE("failed to read executorSensorHint");
93         return READ_PARCEL_ERROR;
94     }
95     if (!data.ReadUint32(executorMatcher)) {
96         IAM_LOGE("failed to read executorMatcher");
97         return READ_PARCEL_ERROR;
98     }
99     if (!data.ReadInt32(esl)) {
100         IAM_LOGE("failed to read esl");
101         return READ_PARCEL_ERROR;
102     }
103     if (!data.ReadUInt8Vector(&executorInfo.publicKey)) {
104         IAM_LOGE("failed to read publicKey");
105         return READ_PARCEL_ERROR;
106     }
107 
108     executorInfo.authType = static_cast<AuthType>(authType);
109     executorInfo.executorRole = static_cast<ExecutorRole>(executorRole);
110     executorInfo.executorSensorHint = executorSensorHint;
111     executorInfo.executorMatcher = executorMatcher;
112     executorInfo.esl = static_cast<ExecutorSecureLevel>(esl);
113     return SUCCESS;
114 }
115 } // namespace UserAuth
116 } // namespace UserIam
117 } // namespace OHOS