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