1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "common/libs/security/gatekeeper_channel_sharedfd.h"
18
19 #include <android-base/logging.h>
20 #include "keymaster/android_keymaster_utils.h"
21
22 namespace cuttlefish {
23
SharedFdGatekeeperChannel(SharedFD input,SharedFD output)24 SharedFdGatekeeperChannel::SharedFdGatekeeperChannel(SharedFD input,
25 SharedFD output)
26 : channel_(transport::SharedFdChannel(std::move(input), std::move(output))) {}
27
SendRequest(uint32_t command,const gatekeeper::GateKeeperMessage & message)28 bool SharedFdGatekeeperChannel::SendRequest(
29 uint32_t command, const gatekeeper::GateKeeperMessage& message) {
30 return SendMessage(command, false, message);
31 }
32
SendResponse(uint32_t command,const gatekeeper::GateKeeperMessage & message)33 bool SharedFdGatekeeperChannel::SendResponse(
34 uint32_t command, const gatekeeper::GateKeeperMessage& message) {
35 return SendMessage(command, true, message);
36 }
37
SendMessage(uint32_t command,bool is_response,const gatekeeper::GateKeeperMessage & message)38 bool SharedFdGatekeeperChannel::SendMessage(uint32_t command, bool is_response,
39 const gatekeeper::GateKeeperMessage& message) {
40 LOG(DEBUG) << "Sending message with id: " << command;
41 auto payload_size = message.GetSerializedSize();
42 auto to_send_result = transport::CreateMessage(command, payload_size);
43 if (!to_send_result.ok()) {
44 LOG(ERROR) << "Could not allocate Gatekeeper Message: "
45 << to_send_result.error().FormatForEnv();
46 return false;
47 }
48 auto to_send = std::move(to_send_result.value());
49 message.Serialize(to_send->payload, to_send->payload + payload_size);
50
51 auto result = is_response ? channel_.SendResponse(*to_send) : channel_.SendRequest(*to_send);
52 if (!result.ok()) {
53 LOG(ERROR) << "Could not write Gatekeeper Message: "
54 << result.error().FormatForEnv();
55 }
56 return result.ok();
57 }
58
ReceiveMessage()59 transport::ManagedMessage SharedFdGatekeeperChannel::ReceiveMessage() {
60 auto result = channel_.ReceiveMessage();
61 if (!result.ok()) {
62 return {};
63 }
64 return std::move(result.value());
65 }
66
67 } // namespace cuttlefish
68