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