• 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 <cstddef>
20 #include <cstdint>
21 #include <memory>
22 
23 #include <keymaster/android_keymaster_messages.h>
24 #include <keymaster/serializable.h>
25 
26 #include "common/libs/fs/shared_fd.h"
27 
28 namespace keymaster {
29 
30 /**
31  * keymaster_message - Serial header for communicating with KM server
32  * @cmd: the command, one of AndroidKeymasterCommand.
33  * @payload: start of the serialized command specific payload
34  */
35 struct keymaster_message {
36     AndroidKeymasterCommand cmd : 31;
37     bool is_response : 1;
38     std::uint32_t payload_size;
39     std::uint8_t payload[0];
40 };
41 
42 } // namespace keymaster
43 
44 namespace cuttlefish {
45 
46 using keymaster::AndroidKeymasterCommand;
47 using keymaster::keymaster_message;
48 
49 /**
50  * A destroyer for keymaster_message instances created with
51  * CreateKeymasterMessage. Wipes memory from the keymaster_message instances.
52  */
53 class KeymasterCommandDestroyer {
54 public:
55   void operator()(keymaster_message* ptr);
56 };
57 
58 /** An owning pointer for a keymaster_message instance. */
59 using ManagedKeymasterMessage =
60     std::unique_ptr<keymaster_message, KeymasterCommandDestroyer>;
61 
62 /**
63  * Allocates memory for a keymaster_message carrying a message of size
64  * `payload_size`.
65  */
66 ManagedKeymasterMessage CreateKeymasterMessage(AndroidKeymasterCommand command,
67                                                bool is_response,
68                                                std::size_t payload_size);
69 
70 /*
71  * Interface for communication channels that synchronously communicate Keymaster
72  * IPC/RPC calls. Sends messages over a file descriptor.
73  */
74 class KeymasterChannel {
75 public:
76   KeymasterChannel(SharedFD input, SharedFD output);
77 
78   bool SendRequest(AndroidKeymasterCommand command,
79                    const keymaster::Serializable& message);
80   bool SendResponse(AndroidKeymasterCommand command,
81                     const keymaster::Serializable& message);
82   ManagedKeymasterMessage ReceiveMessage();
83 private:
84   SharedFD input_;
85   SharedFD output_;
86   bool SendMessage(AndroidKeymasterCommand command, bool response,
87                    const keymaster::Serializable& message);
88 };
89 
90 } // namespace cuttlefish
91