• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdlib>
20 
21 #include <android-base/logging.h>
22 #include "keymaster/android_keymaster_utils.h"
23 
24 #include "common/libs/fs/shared_buf.h"
25 
26 namespace cuttlefish {
27 using gatekeeper::GatekeeperRawMessage;
28 
SharedFdGatekeeperChannel(SharedFD input,SharedFD output)29 SharedFdGatekeeperChannel::SharedFdGatekeeperChannel(SharedFD input,
30                                                      SharedFD output)
31     : input_(input), output_(output) {}
32 
SendRequest(uint32_t command,const gatekeeper::GateKeeperMessage & message)33 bool SharedFdGatekeeperChannel::SendRequest(
34     uint32_t command, const gatekeeper::GateKeeperMessage& message) {
35   return SendMessage(command, false, message);
36 }
37 
SendResponse(uint32_t command,const gatekeeper::GateKeeperMessage & message)38 bool SharedFdGatekeeperChannel::SendResponse(
39     uint32_t command, const gatekeeper::GateKeeperMessage& message) {
40   return SendMessage(command, true, message);
41 }
42 
SendMessage(uint32_t command,bool is_response,const gatekeeper::GateKeeperMessage & message)43 bool SharedFdGatekeeperChannel::SendMessage(
44     uint32_t command, bool is_response,
45     const gatekeeper::GateKeeperMessage& message) {
46   LOG(DEBUG) << "Sending message with id: " << command;
47   auto payload_size = message.GetSerializedSize();
48   auto to_send = CreateGatekeeperMessage(command, is_response, payload_size);
49   message.Serialize(to_send->payload, to_send->payload + payload_size);
50   auto write_size = payload_size + sizeof(GatekeeperRawMessage);
51   auto to_send_bytes = reinterpret_cast<const char*>(to_send.get());
52   auto written = WriteAll(output_, to_send_bytes, write_size);
53   if (written == -1) {
54     LOG(ERROR) << "Could not write Gatekeeper Message: " << output_->StrError();
55   }
56   return written == write_size;
57 }
58 
ReceiveMessage()59 ManagedGatekeeperMessage SharedFdGatekeeperChannel::ReceiveMessage() {
60   struct GatekeeperRawMessage message_header;
61   auto read = ReadExactBinary(input_, &message_header);
62   if (read != sizeof(GatekeeperRawMessage)) {
63     LOG(ERROR) << "Expected " << sizeof(GatekeeperRawMessage) << ", received "
64                << read;
65     LOG(ERROR) << "Could not read Gatekeeper Message: " << input_->StrError();
66     return {};
67   }
68   LOG(DEBUG) << "Received message with id: " << message_header.cmd;
69   auto message =
70       CreateGatekeeperMessage(message_header.cmd, message_header.is_response,
71                               message_header.payload_size);
72   auto message_bytes = reinterpret_cast<char*>(message->payload);
73   read = ReadExact(input_, message_bytes, message->payload_size);
74   if (read != message->payload_size) {
75     LOG(ERROR) << "Could not read Gatekeeper Message: " << input_->StrError();
76     return {};
77   }
78   return message;
79 }
80 
81 }  // namespace cuttlefish