Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 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 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 19 | #include "TrustyIpc.h" |
| 20 | |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 22 | #include <android-base/unique_fd.h> |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <poll.h> |
| 25 | #include <stdio.h> |
| 26 | #include <sys/eventfd.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <teeui/msg_formatting.h> |
| 29 | #include <trusty/tipc.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <fstream> |
| 33 | #include <functional> |
| 34 | #include <future> |
| 35 | #include <iostream> |
| 36 | #include <sstream> |
| 37 | #include <thread> |
| 38 | #include <vector> |
| 39 | |
| 40 | #define AT __FILE__ ":" << __LINE__ << ": " |
| 41 | |
| 42 | namespace android { |
| 43 | namespace trusty { |
| 44 | |
| 45 | using ::teeui::Message; |
| 46 | using ::teeui::msg2tuple_t; |
| 47 | using ::teeui::ReadStream; |
| 48 | using ::teeui::WriteStream; |
| 49 | |
| 50 | #ifndef TEEUI_USE_STD_VECTOR |
| 51 | /* |
| 52 | * TEEUI_USE_STD_VECTOR makes certain wire types like teeui::MsgString and |
| 53 | * teeui::MsgVector be aliases for std::vector. This is required for thread safe |
| 54 | * message serialization. Always compile this with -DTEEUI_USE_STD_VECTOR set in |
| 55 | * CFLAGS of the HAL service. |
| 56 | */ |
| 57 | #error "Must be compiled with -DTEEUI_USE_STD_VECTOR." |
| 58 | #endif |
| 59 | |
| 60 | enum class TrustyAppError : int32_t { |
| 61 | OK, |
| 62 | ERROR = -1, |
| 63 | MSG_TOO_LONG = -2, |
| 64 | }; |
| 65 | |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 66 | class TrustyApp { |
| 67 | private: |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 68 | android::base::unique_fd handle_; |
| 69 | void* shm_base_; |
| 70 | size_t shm_len_; |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 71 | static constexpr const int kInvalidHandle = -1; |
| 72 | /* |
| 73 | * This mutex serializes communication with the trusted app, not handle_. |
| 74 | * Calling issueCmd during construction or deletion is undefined behavior. |
| 75 | */ |
| 76 | std::mutex mutex_; |
| 77 | |
| 78 | public: |
| 79 | TrustyApp(const std::string& path, const std::string& appname); |
| 80 | ~TrustyApp(); |
| 81 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 82 | ssize_t TrustyRpc(const uint8_t* obegin, const uint8_t* oend, uint8_t* ibegin, uint8_t* iend); |
| 83 | |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 84 | template <typename Request, typename Response, typename... T> |
| 85 | std::tuple<TrustyAppError, msg2tuple_t<Response>> issueCmd(const T&... args) { |
| 86 | std::lock_guard<std::mutex> lock(mutex_); |
| 87 | |
| 88 | if (handle_ == kInvalidHandle) { |
| 89 | LOG(ERROR) << "TrustyApp not connected"; |
| 90 | return {TrustyAppError::ERROR, {}}; |
| 91 | } |
| 92 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 93 | uint8_t buffer[CONFIRMATIONUI_MAX_MSG_SIZE]; |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 94 | WriteStream out(buffer); |
| 95 | |
| 96 | out = write(Request(), out, args...); |
| 97 | if (!out) { |
| 98 | LOG(ERROR) << AT << "send command failed: message formatting"; |
| 99 | return {TrustyAppError::MSG_TOO_LONG, {}}; |
| 100 | } |
| 101 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 102 | auto rc = TrustyRpc(&buffer[0], const_cast<const uint8_t*>(out.pos()), &buffer[0], |
| 103 | &buffer[CONFIRMATIONUI_MAX_MSG_SIZE]); |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 104 | if (rc < 0) return {TrustyAppError::ERROR, {}}; |
| 105 | |
| 106 | ReadStream in(&buffer[0], rc); |
| 107 | auto result = read(Response(), in); |
| 108 | if (!std::get<0>(result)) { |
| 109 | LOG(ERROR) << "send command failed: message parsing"; |
| 110 | return {TrustyAppError::ERROR, {}}; |
| 111 | } |
| 112 | |
| 113 | return {std::get<0>(result) ? TrustyAppError::OK : TrustyAppError::ERROR, |
| 114 | tuple_tail(std::move(result))}; |
| 115 | } |
| 116 | |
| 117 | template <typename Request, typename... T> TrustyAppError issueCmd(const T&... args) { |
| 118 | std::lock_guard<std::mutex> lock(mutex_); |
| 119 | |
| 120 | if (handle_ == kInvalidHandle) { |
| 121 | LOG(ERROR) << "TrustyApp not connected"; |
| 122 | return TrustyAppError::ERROR; |
| 123 | } |
| 124 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 125 | uint8_t buffer[CONFIRMATIONUI_MAX_MSG_SIZE]; |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 126 | WriteStream out(buffer); |
| 127 | |
| 128 | out = write(Request(), out, args...); |
| 129 | if (!out) { |
| 130 | LOG(ERROR) << AT << "send command failed: message formatting"; |
| 131 | return TrustyAppError::MSG_TOO_LONG; |
| 132 | } |
| 133 | |
Tri Vo | 19b62a5 | 2021-02-16 11:51:26 -0800 | [diff] [blame] | 134 | auto rc = TrustyRpc(&buffer[0], const_cast<const uint8_t*>(out.pos()), &buffer[0], |
| 135 | &buffer[CONFIRMATIONUI_MAX_MSG_SIZE]); |
Janis Danisevskis | 8fe0cfb | 2020-01-13 14:24:32 -0800 | [diff] [blame] | 136 | if (rc < 0) { |
| 137 | LOG(ERROR) << "send command failed: " << strerror(errno) << " (" << errno << ")"; |
| 138 | return TrustyAppError::ERROR; |
| 139 | } |
| 140 | |
| 141 | if (rc > 0) { |
| 142 | LOG(ERROR) << "Unexpected non zero length response"; |
| 143 | return TrustyAppError::ERROR; |
| 144 | } |
| 145 | return TrustyAppError::OK; |
| 146 | } |
| 147 | |
| 148 | operator bool() const { return handle_ != kInvalidHandle; } |
| 149 | }; |
| 150 | |
| 151 | } // namespace trusty |
| 152 | } // namespace android |