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