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