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 #pragma once 18 19 #include "gatekeeper/gatekeeper_messages.h" 20 21 #include "common/libs/fs/shared_fd.h" 22 23 #include <memory> 24 25 namespace gatekeeper { 26 27 /** 28 * GatekeeperRawMessage - Header and raw byte payload for a serialized 29 * gatekeeper message. 30 * 31 * @cmd: the command, one of gatekeeper::ENROLL and gatekeeper::VERIFY. 32 * @payload: start of the serialized command specific payload 33 */ 34 struct GatekeeperRawMessage { 35 uint32_t cmd : 31; 36 bool is_response : 1; 37 uint32_t payload_size; 38 uint8_t payload[0]; 39 }; 40 41 } // namespace gatekeeper 42 43 namespace cuttlefish { 44 45 using gatekeeper::GatekeeperRawMessage; 46 47 /** 48 * A destroyer for GatekeeperRawMessage instances created with 49 * CreateGatekeeperMessage. Wipes memory from the GatekeeperRawMessage instances. 50 */ 51 class GatekeeperCommandDestroyer { 52 public: 53 void operator()(GatekeeperRawMessage* ptr); 54 }; 55 56 /** An owning pointer for a GatekeeperRawMessage instance. */ 57 using ManagedGatekeeperMessage = 58 std::unique_ptr<GatekeeperRawMessage, GatekeeperCommandDestroyer>; 59 60 /** 61 * Allocates memory for a GatekeeperRawMessage carrying a message of size 62 * `payload_size`. 63 */ 64 ManagedGatekeeperMessage CreateGatekeeperMessage( 65 uint32_t command, bool is_response, size_t payload_size); 66 67 /* 68 * Interface for communication channels that synchronously communicate Gatekeeper 69 * IPC/RPC calls. Sends messages over a file descriptor. 70 */ 71 class GatekeeperChannel { 72 public: 73 GatekeeperChannel(SharedFD channel); 74 75 bool SendRequest(uint32_t command, 76 const gatekeeper::GateKeeperMessage& message); 77 bool SendResponse(uint32_t command, 78 const gatekeeper::GateKeeperMessage& message); 79 ManagedGatekeeperMessage ReceiveMessage(); 80 private: 81 SharedFD channel_; 82 bool SendMessage(uint32_t command, bool response, 83 const gatekeeper::GateKeeperMessage& message); 84 }; 85 86 } // namespace cuttlefish 87