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 "executor_messenger_stub.h"
17 
18 #include <cinttypes>
19 
20 #include "iam_check.h"
21 #include "iam_logger.h"
22 #include "iam_ptr.h"
23 #include "iam_common_defines.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.
ExecutorMessengerStub()32 ExecutorMessengerStub::ExecutorMessengerStub() : IRemoteStub(true) {};
33 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int32_t ExecutorMessengerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
35     MessageOption &option)
36 {
37     IAM_LOGD("ExecutorMessengerStub::OnRemoteRequest, cmd = %{public}u, flags = %{public}d", code, option.GetFlags());
38     if (ExecutorMessengerStub::GetDescriptor() != data.ReadInterfaceToken()) {
39         IAM_LOGE("descriptor is not matched");
40         return GENERAL_ERROR;
41     }
42     switch (code) {
43         case ExecutorMessengerInterfaceCode::CO_AUTH_SEND_DATA:
44             return SendDataStub(data, reply);
45         case ExecutorMessengerInterfaceCode::CO_AUTH_FINISH:
46             return FinishStub(data, reply);
47         default:
48             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
49     }
50 }
51 
SendDataStub(MessageParcel & data,MessageParcel & reply)52 int32_t ExecutorMessengerStub::SendDataStub(MessageParcel &data, MessageParcel &reply)
53 {
54     uint64_t scheduleId;
55     int32_t dstRole;
56     std::vector<uint8_t> msg;
57 
58     if (!data.ReadUint64(scheduleId)) {
59         IAM_LOGE("read scheduleId failed");
60         return READ_PARCEL_ERROR;
61     }
62     if (!data.ReadInt32(dstRole)) {
63         IAM_LOGE("read dstRole failed");
64         return READ_PARCEL_ERROR;
65     }
66     if (!data.ReadUInt8Vector(&msg)) {
67         IAM_LOGE("read msg failed");
68         return GENERAL_ERROR;
69     }
70 
71     int32_t result = SendData(scheduleId, static_cast<ExecutorRole>(dstRole), msg);
72     if (!reply.WriteInt32(result)) {
73         IAM_LOGE("write SendData result failed");
74         return WRITE_PARCEL_ERROR;
75     }
76     return SUCCESS;
77 }
78 
FinishStub(MessageParcel & data,MessageParcel & reply)79 int32_t ExecutorMessengerStub::FinishStub(MessageParcel &data, MessageParcel &reply)
80 {
81     uint64_t scheduleId;
82     int32_t resultCode;
83     std::vector<uint8_t> attributes;
84 
85     if (!data.ReadUint64(scheduleId)) {
86         IAM_LOGE("read scheduleId failed");
87         return READ_PARCEL_ERROR;
88     }
89     if (!data.ReadInt32(resultCode)) {
90         IAM_LOGE("read resultCode failed");
91         return READ_PARCEL_ERROR;
92     }
93     if (!data.ReadUInt8Vector(&attributes)) {
94         IAM_LOGE("read attributes failed");
95         return READ_PARCEL_ERROR;
96     }
97     auto finalResult = Common::MakeShared<Attributes>(attributes);
98     IF_FALSE_LOGE_AND_RETURN_VAL(finalResult != nullptr, WRITE_PARCEL_ERROR);
99     int32_t result =
100         Finish(scheduleId, static_cast<ResultCode>(resultCode), finalResult);
101     if (!reply.WriteInt32(result)) {
102         IAM_LOGE("write Finish result failed");
103         return WRITE_PARCEL_ERROR;
104     }
105     return SUCCESS;
106 }
107 } // namespace UserAuth
108 } // namespace UserIam
109 } // namespace OHOS