Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 "apc_compat.hpp" |
| 18 | #include <android-base/logging.h> |
| 19 | #include <android/hardware/confirmationui/1.0/IConfirmationUI.h> |
| 20 | #include <hwbinder/IBinder.h> |
| 21 | |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 22 | #include <aidl/android/hardware/confirmationui/BnConfirmationResultCallback.h> |
| 23 | #include <aidl/android/hardware/confirmationui/IConfirmationResultCallback.h> |
| 24 | #include <aidl/android/hardware/confirmationui/IConfirmationUI.h> |
| 25 | #include <aidl/android/hardware/confirmationui/UIOption.h> |
| 26 | #include <android/binder_manager.h> |
| 27 | |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 28 | #include <memory> |
Devin Moore | a17c771 | 2024-04-11 21:57:03 +0000 | [diff] [blame] | 29 | #include <set> |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <thread> |
| 32 | #include <vector> |
| 33 | |
| 34 | #define LOG_TAG "keystore2_apc_compat" |
| 35 | |
| 36 | namespace keystore2 { |
| 37 | |
| 38 | using android::sp; |
| 39 | using android::hardware::hidl_death_recipient; |
| 40 | using android::hardware::hidl_vec; |
| 41 | using android::hardware::Return; |
| 42 | using android::hardware::Status; |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 43 | using HidlConfirmationResultCb = |
| 44 | android::hardware::confirmationui::V1_0::IConfirmationResultCallback; |
| 45 | using HidlConfirmationUI = android::hardware::confirmationui::V1_0::IConfirmationUI; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 46 | using android::hardware::confirmationui::V1_0::ResponseCode; |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 47 | using HidlUIOptions = android::hardware::confirmationui::V1_0::UIOption; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 48 | |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 49 | using AidlConfirmationUI = ::aidl::android::hardware::confirmationui::IConfirmationUI; |
| 50 | using AidlBnConfirmationResultCb = |
| 51 | ::aidl::android::hardware::confirmationui::BnConfirmationResultCallback; |
| 52 | using AidlUIOptions = ::aidl::android::hardware::confirmationui::UIOption; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 53 | |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 54 | class CompatSessionCB { |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 55 | public: |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 56 | void |
| 57 | finalize(uint32_t responseCode, ApcCompatCallback callback, |
| 58 | std::optional<std::reference_wrapper<const std::vector<uint8_t>>> dataConfirmed, |
| 59 | std::optional<std::reference_wrapper<const std::vector<uint8_t>>> confirmationToken) { |
| 60 | if (callback.result != nullptr) { |
| 61 | size_t dataConfirmedSize = 0; |
| 62 | const uint8_t* dataConfirmedPtr = nullptr; |
| 63 | size_t confirmationTokenSize = 0; |
| 64 | const uint8_t* confirmationTokenPtr = nullptr; |
| 65 | if (responseCode == APC_COMPAT_ERROR_OK) { |
| 66 | if (dataConfirmed) { |
| 67 | dataConfirmedPtr = dataConfirmed->get().data(); |
| 68 | dataConfirmedSize = dataConfirmed->get().size(); |
| 69 | } |
| 70 | if (confirmationToken) { |
| 71 | confirmationTokenPtr = confirmationToken->get().data(); |
| 72 | confirmationTokenSize = confirmationToken->get().size(); |
| 73 | } |
| 74 | } |
| 75 | callback.result(callback.data, responseCode, dataConfirmedPtr, dataConfirmedSize, |
| 76 | confirmationTokenPtr, confirmationTokenSize); |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | class ConfuiHidlCompatSession : public HidlConfirmationResultCb, |
| 82 | public hidl_death_recipient, |
| 83 | public CompatSessionCB { |
| 84 | public: |
| 85 | static sp<ConfuiHidlCompatSession> tryGetService() { |
| 86 | sp<HidlConfirmationUI> service = HidlConfirmationUI::tryGetService(); |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 87 | if (service) { |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 88 | return sp<ConfuiHidlCompatSession>(new ConfuiHidlCompatSession(std::move(service))); |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 89 | } else { |
| 90 | return nullptr; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | uint32_t promptUserConfirmation(ApcCompatCallback callback, const char* prompt_text, |
| 95 | const uint8_t* extra_data, size_t extra_data_size, |
| 96 | const char* locale, ApcCompatUiOptions ui_options) { |
| 97 | std::string hidl_prompt(prompt_text); |
| 98 | std::vector<uint8_t> hidl_extra(extra_data, extra_data + extra_data_size); |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 99 | std::vector<HidlUIOptions> hidl_ui_options; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 100 | if (ui_options.inverted) { |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 101 | hidl_ui_options.push_back(HidlUIOptions::AccessibilityInverted); |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 102 | } |
| 103 | if (ui_options.magnified) { |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 104 | hidl_ui_options.push_back(HidlUIOptions::AccessibilityMagnified); |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 105 | } |
| 106 | auto lock = std::lock_guard(callback_lock_); |
| 107 | if (callback_.result != nullptr) { |
| 108 | return APC_COMPAT_ERROR_OPERATION_PENDING; |
| 109 | } |
| 110 | auto err = service_->linkToDeath(sp(this), 0); |
| 111 | if (!err.isOk()) { |
| 112 | LOG(ERROR) << "Communication error: promptUserConfirmation: " |
| 113 | "Trying to register death recipient: " |
| 114 | << err.description(); |
| 115 | return APC_COMPAT_ERROR_SYSTEM_ERROR; |
| 116 | } |
| 117 | |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 118 | auto rc = service_->promptUserConfirmation(sp(this), hidl_prompt, hidl_extra, locale, |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 119 | hidl_ui_options); |
| 120 | if (!rc.isOk()) { |
| 121 | LOG(ERROR) << "Communication error: promptUserConfirmation: " << rc.description(); |
Shaquille Johnson | 718036a | 2023-08-10 15:16:43 +0100 | [diff] [blame] | 122 | } else if (rc == ResponseCode::OK) { |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 123 | callback_ = callback; |
| 124 | } |
| 125 | return responseCode2Compat(rc.withDefault(ResponseCode::SystemError)); |
| 126 | } |
| 127 | |
| 128 | void abort() { service_->abort(); } |
| 129 | |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 130 | void finalize(ResponseCode responseCode, const hidl_vec<uint8_t>& dataConfirmed, |
| 131 | const hidl_vec<uint8_t>& confirmationToken) { |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 132 | ApcCompatCallback callback; |
| 133 | { |
| 134 | auto lock = std::lock_guard(callback_lock_); |
| 135 | // Calling the callback consumes the callback data structure. We have to make |
| 136 | // sure that it can only be called once. |
| 137 | callback = callback_; |
| 138 | callback_ = {nullptr, nullptr}; |
| 139 | // Unlock the callback_lock_ here. It must never be held while calling the callback. |
| 140 | } |
| 141 | |
| 142 | if (callback.result != nullptr) { |
| 143 | service_->unlinkToDeath(sp(this)); |
| 144 | |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 145 | std::vector<uint8_t> data = dataConfirmed; |
| 146 | std::vector<uint8_t> token = confirmationToken; |
| 147 | |
| 148 | CompatSessionCB::finalize(responseCode2Compat(responseCode), callback, data, token); |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 152 | // HidlConfirmationResultCb overrides: |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 153 | android::hardware::Return<void> result(ResponseCode responseCode, |
| 154 | const hidl_vec<uint8_t>& dataConfirmed, |
| 155 | const hidl_vec<uint8_t>& confirmationToken) override { |
| 156 | finalize(responseCode, dataConfirmed, confirmationToken); |
| 157 | return Status::ok(); |
| 158 | }; |
| 159 | |
| 160 | void serviceDied(uint64_t /* cookie */, |
| 161 | const ::android::wp<::android::hidl::base::V1_0::IBase>& /* who */) override { |
| 162 | finalize(ResponseCode::SystemError, {}, {}); |
| 163 | } |
| 164 | |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 165 | static uint32_t responseCode2Compat(ResponseCode rc) { |
| 166 | switch (rc) { |
| 167 | case ResponseCode::OK: |
| 168 | return APC_COMPAT_ERROR_OK; |
| 169 | case ResponseCode::Canceled: |
| 170 | return APC_COMPAT_ERROR_CANCELLED; |
| 171 | case ResponseCode::Aborted: |
| 172 | return APC_COMPAT_ERROR_ABORTED; |
| 173 | case ResponseCode::OperationPending: |
| 174 | return APC_COMPAT_ERROR_OPERATION_PENDING; |
| 175 | case ResponseCode::Ignored: |
| 176 | return APC_COMPAT_ERROR_IGNORED; |
| 177 | case ResponseCode::SystemError: |
| 178 | case ResponseCode::Unimplemented: |
| 179 | case ResponseCode::Unexpected: |
| 180 | case ResponseCode::UIError: |
| 181 | case ResponseCode::UIErrorMissingGlyph: |
| 182 | case ResponseCode::UIErrorMessageTooLong: |
| 183 | case ResponseCode::UIErrorMalformedUTF8Encoding: |
| 184 | default: |
| 185 | return APC_COMPAT_ERROR_SYSTEM_ERROR; |
| 186 | } |
| 187 | } |
| 188 | |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 189 | private: |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 190 | ConfuiHidlCompatSession(sp<HidlConfirmationUI> service) |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 191 | : service_(service), callback_{nullptr, nullptr} {} |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 192 | sp<HidlConfirmationUI> service_; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 193 | |
| 194 | // The callback_lock_ protects the callback_ field against concurrent modification. |
| 195 | // IMPORTANT: It must never be held while calling the call back. |
| 196 | std::mutex callback_lock_; |
| 197 | ApcCompatCallback callback_; |
| 198 | }; |
| 199 | |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 200 | class ConfuiAidlCompatSession : public AidlBnConfirmationResultCb, public CompatSessionCB { |
| 201 | public: |
| 202 | static std::shared_ptr<ConfuiAidlCompatSession> tryGetService() { |
| 203 | constexpr const char confirmationUIServiceName[] = |
| 204 | "android.hardware.confirmationui.IConfirmationUI/default"; |
| 205 | if (!AServiceManager_isDeclared(confirmationUIServiceName)) { |
| 206 | LOG(INFO) << confirmationUIServiceName << " is not declared in VINTF"; |
| 207 | return nullptr; |
| 208 | } |
| 209 | std::shared_ptr<AidlConfirmationUI> aidlService = AidlConfirmationUI::fromBinder( |
| 210 | ndk::SpAIBinder(AServiceManager_waitForService(confirmationUIServiceName))); |
| 211 | if (aidlService) { |
| 212 | return ::ndk::SharedRefBase::make<ConfuiAidlCompatSession>(aidlService); |
| 213 | } |
| 214 | |
| 215 | return nullptr; |
| 216 | } |
| 217 | |
Devin Moore | a17c771 | 2024-04-11 21:57:03 +0000 | [diff] [blame] | 218 | class DeathRecipientCookie { |
| 219 | public: |
| 220 | DeathRecipientCookie(std::weak_ptr<ConfuiAidlCompatSession> session) |
| 221 | : mAidlSession(session) {} |
| 222 | DeathRecipientCookie() = delete; |
| 223 | std::weak_ptr<ConfuiAidlCompatSession> mAidlSession; |
| 224 | }; |
| 225 | |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 226 | uint32_t promptUserConfirmation(ApcCompatCallback callback, const char* prompt_text, |
| 227 | const uint8_t* extra_data, size_t extra_data_size, |
| 228 | const char* locale, ApcCompatUiOptions ui_options) { |
| 229 | std::vector<uint8_t> aidl_prompt(prompt_text, prompt_text + strlen(prompt_text)); |
| 230 | std::vector<uint8_t> aidl_extra(extra_data, extra_data + extra_data_size); |
| 231 | std::vector<AidlUIOptions> aidl_ui_options; |
| 232 | if (ui_options.inverted) { |
| 233 | aidl_ui_options.push_back(AidlUIOptions::ACCESSIBILITY_INVERTED); |
| 234 | } |
| 235 | if (ui_options.magnified) { |
| 236 | aidl_ui_options.push_back(AidlUIOptions::ACCESSIBILITY_MAGNIFIED); |
| 237 | } |
| 238 | auto lock = std::lock_guard(callback_lock_); |
| 239 | if (callback_.result != nullptr) { |
| 240 | return APC_COMPAT_ERROR_OPERATION_PENDING; |
| 241 | } |
| 242 | |
| 243 | if (!aidlService_) { |
| 244 | return APC_COMPAT_ERROR_SYSTEM_ERROR; |
| 245 | } |
Devin Moore | a17c771 | 2024-04-11 21:57:03 +0000 | [diff] [blame] | 246 | |
| 247 | { |
| 248 | auto cookieLock = std::lock_guard(deathRecipientCookie_lock_); |
| 249 | void* cookie = new DeathRecipientCookie(this->ref<ConfuiAidlCompatSession>()); |
| 250 | auto linkRet = AIBinder_linkToDeath(aidlService_->asBinder().get(), |
| 251 | death_recipient_.get(), cookie); |
| 252 | if (linkRet != STATUS_OK) { |
| 253 | LOG(ERROR) << "Communication error: promptUserConfirmation: " |
| 254 | "Trying to register death recipient: "; |
| 255 | delete static_cast<DeathRecipientCookie*>(cookie); |
| 256 | return APC_COMPAT_ERROR_SYSTEM_ERROR; |
| 257 | } |
| 258 | deathRecipientCookie_.insert(cookie); |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | auto rc = aidlService_->promptUserConfirmation(ref<ConfuiAidlCompatSession>(), aidl_prompt, |
| 262 | aidl_extra, locale, aidl_ui_options); |
| 263 | int ret = getReturnCode(rc); |
| 264 | if (ret == AidlConfirmationUI::OK) { |
| 265 | callback_ = callback; |
| 266 | } else { |
| 267 | LOG(ERROR) << "Communication error: promptUserConfirmation: " << rc.getDescription(); |
| 268 | } |
| 269 | return responseCode2Compat(ret); |
| 270 | } |
| 271 | |
| 272 | void abort() { |
| 273 | if (aidlService_) { |
| 274 | aidlService_->abort(); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void |
| 279 | finalize(int32_t responseCode, |
| 280 | std::optional<std::reference_wrapper<const std::vector<uint8_t>>> dataConfirmed, |
| 281 | std::optional<std::reference_wrapper<const std::vector<uint8_t>>> confirmationToken) { |
| 282 | ApcCompatCallback callback; |
| 283 | { |
| 284 | auto lock = std::lock_guard(callback_lock_); |
| 285 | // Calling the callback consumes the callback data structure. We have to make |
| 286 | // sure that it can only be called once. |
| 287 | callback = callback_; |
| 288 | callback_ = {nullptr, nullptr}; |
| 289 | // Unlock the callback_lock_ here. It must never be held while calling the callback. |
| 290 | } |
| 291 | |
| 292 | if (callback.result != nullptr) { |
| 293 | if (aidlService_) { |
Devin Moore | a17c771 | 2024-04-11 21:57:03 +0000 | [diff] [blame] | 294 | // unlink all of the registered death recipients in case there |
| 295 | // were multiple calls to promptUserConfirmation before a call |
| 296 | // to finalize |
| 297 | std::set<void*> cookiesToUnlink; |
| 298 | { |
| 299 | auto cookieLock = std::lock_guard(deathRecipientCookie_lock_); |
| 300 | cookiesToUnlink = deathRecipientCookie_; |
| 301 | deathRecipientCookie_.clear(); |
| 302 | } |
| 303 | |
| 304 | // Unlink these outside of the lock |
| 305 | for (void* cookie : cookiesToUnlink) { |
| 306 | AIBinder_unlinkToDeath(aidlService_->asBinder().get(), death_recipient_.get(), |
| 307 | cookie); |
| 308 | } |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 309 | } |
| 310 | CompatSessionCB::finalize(responseCode2Compat(responseCode), callback, dataConfirmed, |
| 311 | confirmationToken); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // AidlBnConfirmationResultCb overrides: |
| 316 | ::ndk::ScopedAStatus result(int32_t responseCode, const std::vector<uint8_t>& dataConfirmed, |
| 317 | const std::vector<uint8_t>& confirmationToken) override { |
| 318 | finalize(responseCode, dataConfirmed, confirmationToken); |
| 319 | return ::ndk::ScopedAStatus::ok(); |
| 320 | }; |
| 321 | |
| 322 | void serviceDied() { |
| 323 | aidlService_.reset(); |
| 324 | aidlService_ = nullptr; |
Devin Moore | a17c771 | 2024-04-11 21:57:03 +0000 | [diff] [blame] | 325 | { |
| 326 | std::lock_guard lock(deathRecipientCookie_lock_); |
| 327 | deathRecipientCookie_.clear(); |
| 328 | } |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 329 | finalize(AidlConfirmationUI::SYSTEM_ERROR, {}, {}); |
| 330 | } |
| 331 | |
Devin Moore | a17c771 | 2024-04-11 21:57:03 +0000 | [diff] [blame] | 332 | void serviceUnlinked(void* cookie) { |
| 333 | { |
| 334 | std::lock_guard lock(deathRecipientCookie_lock_); |
| 335 | deathRecipientCookie_.erase(cookie); |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 336 | } |
Devin Moore | a17c771 | 2024-04-11 21:57:03 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static void binderDiedCallbackAidl(void* ptr) { |
| 340 | auto aidlSessionCookie = static_cast<ConfuiAidlCompatSession::DeathRecipientCookie*>(ptr); |
| 341 | if (aidlSessionCookie == nullptr) { |
| 342 | LOG(ERROR) << __func__ << ": Null cookie for binderDiedCallbackAidl when HAL died!"; |
| 343 | return; |
| 344 | } else if (auto aidlSession = aidlSessionCookie->mAidlSession.lock(); |
| 345 | aidlSession != nullptr) { |
| 346 | LOG(WARNING) << __func__ << " : Notififying ConfuiAidlCompatSession Service died."; |
| 347 | aidlSession->serviceDied(); |
| 348 | } else { |
| 349 | LOG(ERROR) << __func__ |
| 350 | << " : ConfuiAidlCompatSession Service died but object is no longer around " |
| 351 | "to be able to notify."; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | static void binderUnlinkedCallbackAidl(void* ptr) { |
| 356 | auto aidlSessionCookie = static_cast<ConfuiAidlCompatSession::DeathRecipientCookie*>(ptr); |
| 357 | if (aidlSessionCookie == nullptr) { |
| 358 | LOG(ERROR) << __func__ << ": Null cookie!"; |
| 359 | return; |
| 360 | } else if (auto aidlSession = aidlSessionCookie->mAidlSession.lock(); |
| 361 | aidlSession != nullptr) { |
| 362 | aidlSession->serviceUnlinked(ptr); |
| 363 | } |
| 364 | delete aidlSessionCookie; |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | int getReturnCode(const ::ndk::ScopedAStatus& result) { |
| 368 | if (result.isOk()) return AidlConfirmationUI::OK; |
| 369 | |
| 370 | if (result.getExceptionCode() == EX_SERVICE_SPECIFIC) { |
| 371 | return static_cast<int>(result.getServiceSpecificError()); |
| 372 | } |
| 373 | return result.getStatus(); |
| 374 | } |
| 375 | |
| 376 | uint32_t responseCode2Compat(int32_t rc) { |
| 377 | switch (rc) { |
| 378 | case AidlConfirmationUI::OK: |
| 379 | return APC_COMPAT_ERROR_OK; |
| 380 | case AidlConfirmationUI::CANCELED: |
| 381 | return APC_COMPAT_ERROR_CANCELLED; |
| 382 | case AidlConfirmationUI::ABORTED: |
| 383 | return APC_COMPAT_ERROR_ABORTED; |
| 384 | case AidlConfirmationUI::OPERATION_PENDING: |
| 385 | return APC_COMPAT_ERROR_OPERATION_PENDING; |
| 386 | case AidlConfirmationUI::IGNORED: |
| 387 | return APC_COMPAT_ERROR_IGNORED; |
| 388 | case AidlConfirmationUI::SYSTEM_ERROR: |
| 389 | case AidlConfirmationUI::UNIMPLEMENTED: |
| 390 | case AidlConfirmationUI::UNEXPECTED: |
| 391 | case AidlConfirmationUI::UI_ERROR: |
| 392 | case AidlConfirmationUI::UI_ERROR_MISSING_GLYPH: |
| 393 | case AidlConfirmationUI::UI_ERROR_MESSAGE_TOO_LONG: |
| 394 | case AidlConfirmationUI::UI_ERROR_MALFORMED_UTF8ENCODING: |
| 395 | default: |
| 396 | return APC_COMPAT_ERROR_SYSTEM_ERROR; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | ConfuiAidlCompatSession(std::shared_ptr<AidlConfirmationUI> service) |
| 401 | : aidlService_(service), callback_{nullptr, nullptr} { |
| 402 | death_recipient_ = ::ndk::ScopedAIBinder_DeathRecipient( |
| 403 | AIBinder_DeathRecipient_new(binderDiedCallbackAidl)); |
Devin Moore | a17c771 | 2024-04-11 21:57:03 +0000 | [diff] [blame] | 404 | AIBinder_DeathRecipient_setOnUnlinked(death_recipient_.get(), binderUnlinkedCallbackAidl); |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | virtual ~ConfuiAidlCompatSession() = default; |
| 408 | ConfuiAidlCompatSession(const ConfuiAidlCompatSession&) = delete; |
| 409 | ConfuiAidlCompatSession& operator=(const ConfuiAidlCompatSession&) = delete; |
| 410 | |
| 411 | private: |
| 412 | std::shared_ptr<AidlConfirmationUI> aidlService_; |
Devin Moore | a17c771 | 2024-04-11 21:57:03 +0000 | [diff] [blame] | 413 | std::mutex deathRecipientCookie_lock_; |
| 414 | std::set<void*> deathRecipientCookie_; |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 415 | |
| 416 | // The callback_lock_ protects the callback_ field against concurrent modification. |
| 417 | // IMPORTANT: It must never be held while calling the call back. |
| 418 | std::mutex callback_lock_; |
| 419 | ApcCompatCallback callback_; |
| 420 | |
| 421 | ::ndk::ScopedAIBinder_DeathRecipient death_recipient_; |
| 422 | }; |
| 423 | |
| 424 | class ApcCompatSession { |
| 425 | public: |
| 426 | static ApcCompatServiceHandle getApcCompatSession() { |
| 427 | auto aidlCompatSession = ConfuiAidlCompatSession::tryGetService(); |
| 428 | if (aidlCompatSession) { |
| 429 | return new ApcCompatSession(std::move(aidlCompatSession), nullptr); |
| 430 | } |
| 431 | |
| 432 | sp<ConfuiHidlCompatSession> hidlCompatSession = ConfuiHidlCompatSession::tryGetService(); |
| 433 | if (hidlCompatSession) { |
| 434 | return new ApcCompatSession(nullptr, std::move(hidlCompatSession)); |
| 435 | } |
| 436 | |
| 437 | LOG(ERROR) << "ConfirmationUI: Not found Service"; |
| 438 | return nullptr; |
| 439 | } |
| 440 | |
| 441 | uint32_t promptUserConfirmation(ApcCompatCallback callback, const char* prompt_text, |
| 442 | const uint8_t* extra_data, size_t extra_data_size, |
| 443 | char const* locale, ApcCompatUiOptions ui_options) { |
| 444 | if (aidlCompatSession_) { |
| 445 | return aidlCompatSession_->promptUserConfirmation(callback, prompt_text, extra_data, |
| 446 | extra_data_size, locale, ui_options); |
| 447 | } else { |
| 448 | return hidlCompatSession_->promptUserConfirmation(callback, prompt_text, extra_data, |
| 449 | extra_data_size, locale, ui_options); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void abortUserConfirmation() { |
| 454 | if (aidlCompatSession_) { |
| 455 | return aidlCompatSession_->abort(); |
| 456 | } else { |
| 457 | return hidlCompatSession_->abort(); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | void closeUserConfirmationService() { |
| 462 | // Closing the handle implicitly aborts an ongoing sessions. |
| 463 | // Note that a resulting callback is still safely conducted, because we only delete a |
| 464 | // StrongPointer below. libhwbinder still owns another StrongPointer to this session. |
| 465 | abortUserConfirmation(); |
| 466 | } |
| 467 | |
| 468 | ApcCompatSession(std::shared_ptr<ConfuiAidlCompatSession> aidlCompatSession, |
| 469 | sp<ConfuiHidlCompatSession> hidlCompatSession) |
| 470 | : aidlCompatSession_(aidlCompatSession), hidlCompatSession_(hidlCompatSession) {} |
| 471 | |
| 472 | private: |
| 473 | std::shared_ptr<ConfuiAidlCompatSession> aidlCompatSession_; |
| 474 | sp<ConfuiHidlCompatSession> hidlCompatSession_; |
| 475 | }; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 476 | } // namespace keystore2 |
| 477 | |
| 478 | using namespace keystore2; |
| 479 | |
| 480 | ApcCompatServiceHandle tryGetUserConfirmationService() { |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 481 | return reinterpret_cast<ApcCompatServiceHandle>(ApcCompatSession::getApcCompatSession()); |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | uint32_t promptUserConfirmation(ApcCompatServiceHandle handle, ApcCompatCallback callback, |
| 485 | const char* prompt_text, const uint8_t* extra_data, |
| 486 | size_t extra_data_size, char const* locale, |
| 487 | ApcCompatUiOptions ui_options) { |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 488 | auto session = reinterpret_cast<ApcCompatSession*>(handle); |
| 489 | return session->promptUserConfirmation(callback, prompt_text, extra_data, extra_data_size, |
| 490 | locale, ui_options); |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | void abortUserConfirmation(ApcCompatServiceHandle handle) { |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 494 | auto session = reinterpret_cast<ApcCompatSession*>(handle); |
| 495 | session->abortUserConfirmation(); |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void closeUserConfirmationService(ApcCompatServiceHandle handle) { |
Rajesh Nyamagoud | dc4b612 | 2022-09-14 23:14:29 +0000 | [diff] [blame] | 499 | auto session = reinterpret_cast<ApcCompatSession*>(handle); |
| 500 | session->closeUserConfirmationService(); |
| 501 | delete reinterpret_cast<ApcCompatSession*>(handle); |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | const ApcCompatServiceHandle INVALID_SERVICE_HANDLE = nullptr; |