• 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 #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 input,SharedFD output)43 KeymasterChannel::KeymasterChannel(SharedFD input, SharedFD output)
44     : input_(input), output_(output) {
45 }
46 
SendRequest(AndroidKeymasterCommand command,const keymaster::Serializable & message)47 bool KeymasterChannel::SendRequest(
48     AndroidKeymasterCommand command, const keymaster::Serializable& message) {
49   return SendMessage(command, false, message);
50 }
51 
SendResponse(AndroidKeymasterCommand command,const keymaster::Serializable & message)52 bool KeymasterChannel::SendResponse(
53     AndroidKeymasterCommand command, const keymaster::Serializable& message) {
54   return SendMessage(command, true, message);
55 }
56 
SendMessage(AndroidKeymasterCommand command,bool is_response,const keymaster::Serializable & message)57 bool KeymasterChannel::SendMessage(
58     AndroidKeymasterCommand command,
59     bool is_response,
60     const keymaster::Serializable& message) {
61   auto payload_size = message.SerializedSize();
62   LOG(VERBOSE) << "Sending message with id: " << command << " and size "
63                << payload_size;
64   auto to_send = CreateKeymasterMessage(command, is_response, payload_size);
65   message.Serialize(to_send->payload, to_send->payload + payload_size);
66   auto write_size = payload_size + sizeof(keymaster_message);
67   auto to_send_bytes = reinterpret_cast<const char*>(to_send.get());
68   auto written = WriteAll(output_, to_send_bytes, write_size);
69   if (written != write_size) {
70     LOG(ERROR) << "Could not write Keymaster Message: " << output_->StrError();
71   }
72   return written == write_size;
73 }
74 
ReceiveMessage()75 ManagedKeymasterMessage KeymasterChannel::ReceiveMessage() {
76   struct keymaster_message message_header;
77   auto read = ReadExactBinary(input_, &message_header);
78   if (read != sizeof(keymaster_message)) {
79     LOG(ERROR) << "Expected " << sizeof(keymaster_message) << ", received "
80                << read;
81     LOG(ERROR) << "Could not read Keymaster Message: " << input_->StrError();
82     return {};
83   }
84   LOG(VERBOSE) << "Received message with id: " << message_header.cmd
85                << " and size " << message_header.payload_size;
86   auto message = CreateKeymasterMessage(message_header.cmd,
87                                         message_header.is_response,
88                                         message_header.payload_size);
89   auto message_bytes = reinterpret_cast<char*>(message->payload);
90   read = ReadExact(input_, message_bytes, message->payload_size);
91   if (read != message->payload_size) {
92     LOG(ERROR) << "Could not read Keymaster Message: " << input_->StrError();
93     return {};
94   }
95   return message;
96 }
97 
98 }
99