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 "keymaster_channel.h"
18 
19 #include <android-base/logging.h>
20 #include "keymaster/android_keymaster_utils.h"
21 
22 #include "common/libs/fs/shared_buf.h"
23 
24 namespace cuttlefish {
25 
CreateKeymasterMessage(AndroidKeymasterCommand command,bool is_response,size_t payload_size)26 ManagedKeymasterMessage CreateKeymasterMessage(
27     AndroidKeymasterCommand command, bool is_response, size_t payload_size) {
28   auto memory = new uint8_t[payload_size + sizeof(keymaster_message)];
29   auto message = reinterpret_cast<keymaster_message*>(memory);
30   message->cmd = command;
31   message->is_response = is_response;
32   message->payload_size = payload_size;
33   return ManagedKeymasterMessage(message);
34 }
35 
operator ()(keymaster_message * ptr)36 void KeymasterCommandDestroyer::operator()(keymaster_message* ptr) {
37   {
38     keymaster::Eraser(ptr, sizeof(keymaster_message) + ptr->payload_size);
39   }
40   delete reinterpret_cast<uint8_t*>(ptr);
41 }
42 
KeymasterChannel(SharedFD channel)43 KeymasterChannel::KeymasterChannel(SharedFD channel) : channel_(channel) {
44 }
45 
SendRequest(AndroidKeymasterCommand command,const keymaster::Serializable & message)46 bool KeymasterChannel::SendRequest(
47     AndroidKeymasterCommand command, const keymaster::Serializable& message) {
48   return SendMessage(command, false, message);
49 }
50 
SendResponse(AndroidKeymasterCommand command,const keymaster::Serializable & message)51 bool KeymasterChannel::SendResponse(
52     AndroidKeymasterCommand command, const keymaster::Serializable& message) {
53   return SendMessage(command, true, message);
54 }
55 
SendMessage(AndroidKeymasterCommand command,bool is_response,const keymaster::Serializable & message)56 bool KeymasterChannel::SendMessage(
57     AndroidKeymasterCommand command,
58     bool is_response,
59     const keymaster::Serializable& message) {
60   LOG(DEBUG) << "Sending message with id: " << command;
61   auto payload_size = message.SerializedSize();
62   auto to_send = CreateKeymasterMessage(command, is_response, payload_size);
63   message.Serialize(to_send->payload, to_send->payload + payload_size);
64   auto write_size = payload_size + sizeof(keymaster_message);
65   auto to_send_bytes = reinterpret_cast<const char*>(to_send.get());
66   auto written = WriteAll(channel_, to_send_bytes, write_size);
67   if (written == -1) {
68     LOG(ERROR) << "Could not write Keymaster Message: " << channel_->StrError();
69   }
70   return written == write_size;
71 }
72 
ReceiveMessage()73 ManagedKeymasterMessage KeymasterChannel::ReceiveMessage() {
74   struct keymaster_message message_header;
75   auto read = ReadExactBinary(channel_, &message_header);
76   if (read != sizeof(keymaster_message)) {
77     LOG(ERROR) << "Expected " << sizeof(keymaster_message) << ", received "
78                << read;
79     LOG(ERROR) << "Could not read Keymaster Message: " << channel_->StrError();
80     return {};
81   }
82   LOG(DEBUG) << "Received message with id: " << message_header.cmd;
83   auto message = CreateKeymasterMessage(message_header.cmd,
84                                         message_header.is_response,
85                                         message_header.payload_size);
86   auto message_bytes = reinterpret_cast<char*>(message->payload);
87   read = ReadExact(channel_, message_bytes, message->payload_size);
88   if (read != message->payload_size) {
89     LOG(ERROR) << "Could not read Keymaster Message: " << channel_->StrError();
90     return {};
91   }
92   return message;
93 }
94 
95 }
96