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
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
28 #include <memory>
29 #include <set>
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;
43 using HidlConfirmationResultCb =
44 android::hardware::confirmationui::V1_0::IConfirmationResultCallback;
45 using HidlConfirmationUI = android::hardware::confirmationui::V1_0::IConfirmationUI;
46 using android::hardware::confirmationui::V1_0::ResponseCode;
47 using HidlUIOptions = android::hardware::confirmationui::V1_0::UIOption;
48
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;
53
54 class CompatSessionCB {
55 public:
56 void
finalize(uint32_t responseCode,ApcCompatCallback callback,std::optional<std::reference_wrapper<const std::vector<uint8_t>>> dataConfirmed,std::optional<std::reference_wrapper<const std::vector<uint8_t>>> confirmationToken)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:
tryGetService()85 static sp<ConfuiHidlCompatSession> tryGetService() {
86 sp<HidlConfirmationUI> service = HidlConfirmationUI::tryGetService();
87 if (service) {
88 return sp<ConfuiHidlCompatSession>(new ConfuiHidlCompatSession(std::move(service)));
89 } else {
90 return nullptr;
91 }
92 }
93
promptUserConfirmation(ApcCompatCallback callback,const char * prompt_text,const uint8_t * extra_data,size_t extra_data_size,const char * locale,ApcCompatUiOptions ui_options)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);
99 std::vector<HidlUIOptions> hidl_ui_options;
100 if (ui_options.inverted) {
101 hidl_ui_options.push_back(HidlUIOptions::AccessibilityInverted);
102 }
103 if (ui_options.magnified) {
104 hidl_ui_options.push_back(HidlUIOptions::AccessibilityMagnified);
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
118 auto rc = service_->promptUserConfirmation(sp(this), hidl_prompt, hidl_extra, locale,
119 hidl_ui_options);
120 if (!rc.isOk()) {
121 LOG(ERROR) << "Communication error: promptUserConfirmation: " << rc.description();
122 } else if (rc == ResponseCode::OK) {
123 callback_ = callback;
124 }
125 return responseCode2Compat(rc.withDefault(ResponseCode::SystemError));
126 }
127
abort()128 void abort() { service_->abort(); }
129
finalize(ResponseCode responseCode,const hidl_vec<uint8_t> & dataConfirmed,const hidl_vec<uint8_t> & confirmationToken)130 void finalize(ResponseCode responseCode, const hidl_vec<uint8_t>& dataConfirmed,
131 const hidl_vec<uint8_t>& confirmationToken) {
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
145 std::vector<uint8_t> data = dataConfirmed;
146 std::vector<uint8_t> token = confirmationToken;
147
148 CompatSessionCB::finalize(responseCode2Compat(responseCode), callback, data, token);
149 }
150 }
151
152 // HidlConfirmationResultCb overrides:
result(ResponseCode responseCode,const hidl_vec<uint8_t> & dataConfirmed,const hidl_vec<uint8_t> & confirmationToken)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
serviceDied(uint64_t,const::android::wp<::android::hidl::base::V1_0::IBase> &)160 void serviceDied(uint64_t /* cookie */,
161 const ::android::wp<::android::hidl::base::V1_0::IBase>& /* who */) override {
162 finalize(ResponseCode::SystemError, {}, {});
163 }
164
responseCode2Compat(ResponseCode rc)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
189 private:
ConfuiHidlCompatSession(sp<HidlConfirmationUI> service)190 ConfuiHidlCompatSession(sp<HidlConfirmationUI> service)
191 : service_(service), callback_{nullptr, nullptr} {}
192 sp<HidlConfirmationUI> service_;
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
200 class ConfuiAidlCompatSession : public AidlBnConfirmationResultCb, public CompatSessionCB {
201 public:
tryGetService()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
218 class DeathRecipientCookie {
219 public:
DeathRecipientCookie(std::weak_ptr<ConfuiAidlCompatSession> session)220 DeathRecipientCookie(std::weak_ptr<ConfuiAidlCompatSession> session)
221 : mAidlSession(session) {}
222 DeathRecipientCookie() = delete;
223 std::weak_ptr<ConfuiAidlCompatSession> mAidlSession;
224 };
225
promptUserConfirmation(ApcCompatCallback callback,const char * prompt_text,const uint8_t * extra_data,size_t extra_data_size,const char * locale,ApcCompatUiOptions ui_options)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 }
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);
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
abort()272 void abort() {
273 if (aidlService_) {
274 aidlService_->abort();
275 }
276 }
277
278 void
finalize(int32_t responseCode,std::optional<std::reference_wrapper<const std::vector<uint8_t>>> dataConfirmed,std::optional<std::reference_wrapper<const std::vector<uint8_t>>> confirmationToken)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_) {
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 }
309 }
310 CompatSessionCB::finalize(responseCode2Compat(responseCode), callback, dataConfirmed,
311 confirmationToken);
312 }
313 }
314
315 // AidlBnConfirmationResultCb overrides:
result(int32_t responseCode,const std::vector<uint8_t> & dataConfirmed,const std::vector<uint8_t> & confirmationToken)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
serviceDied()322 void serviceDied() {
323 aidlService_.reset();
324 aidlService_ = nullptr;
325 {
326 std::lock_guard lock(deathRecipientCookie_lock_);
327 deathRecipientCookie_.clear();
328 }
329 finalize(AidlConfirmationUI::SYSTEM_ERROR, {}, {});
330 }
331
serviceUnlinked(void * cookie)332 void serviceUnlinked(void* cookie) {
333 {
334 std::lock_guard lock(deathRecipientCookie_lock_);
335 deathRecipientCookie_.erase(cookie);
336 }
337 }
338
binderDiedCallbackAidl(void * ptr)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
binderUnlinkedCallbackAidl(void * ptr)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;
365 }
366
getReturnCode(const::ndk::ScopedAStatus & result)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
responseCode2Compat(int32_t rc)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
ConfuiAidlCompatSession(std::shared_ptr<AidlConfirmationUI> service)400 ConfuiAidlCompatSession(std::shared_ptr<AidlConfirmationUI> service)
401 : aidlService_(service), callback_{nullptr, nullptr} {
402 death_recipient_ = ::ndk::ScopedAIBinder_DeathRecipient(
403 AIBinder_DeathRecipient_new(binderDiedCallbackAidl));
404 AIBinder_DeathRecipient_setOnUnlinked(death_recipient_.get(), binderUnlinkedCallbackAidl);
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_;
413 std::mutex deathRecipientCookie_lock_;
414 std::set<void*> deathRecipientCookie_;
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:
getApcCompatSession()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
promptUserConfirmation(ApcCompatCallback callback,const char * prompt_text,const uint8_t * extra_data,size_t extra_data_size,char const * locale,ApcCompatUiOptions ui_options)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
abortUserConfirmation()453 void abortUserConfirmation() {
454 if (aidlCompatSession_) {
455 return aidlCompatSession_->abort();
456 } else {
457 return hidlCompatSession_->abort();
458 }
459 }
460
closeUserConfirmationService()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
ApcCompatSession(std::shared_ptr<ConfuiAidlCompatSession> aidlCompatSession,sp<ConfuiHidlCompatSession> hidlCompatSession)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 };
476 } // namespace keystore2
477
478 using namespace keystore2;
479
tryGetUserConfirmationService()480 ApcCompatServiceHandle tryGetUserConfirmationService() {
481 return reinterpret_cast<ApcCompatServiceHandle>(ApcCompatSession::getApcCompatSession());
482 }
483
promptUserConfirmation(ApcCompatServiceHandle handle,ApcCompatCallback callback,const char * prompt_text,const uint8_t * extra_data,size_t extra_data_size,char const * locale,ApcCompatUiOptions ui_options)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) {
488 auto session = reinterpret_cast<ApcCompatSession*>(handle);
489 return session->promptUserConfirmation(callback, prompt_text, extra_data, extra_data_size,
490 locale, ui_options);
491 }
492
abortUserConfirmation(ApcCompatServiceHandle handle)493 void abortUserConfirmation(ApcCompatServiceHandle handle) {
494 auto session = reinterpret_cast<ApcCompatSession*>(handle);
495 session->abortUserConfirmation();
496 }
497
closeUserConfirmationService(ApcCompatServiceHandle handle)498 void closeUserConfirmationService(ApcCompatServiceHandle handle) {
499 auto session = reinterpret_cast<ApcCompatSession*>(handle);
500 session->closeUserConfirmationService();
501 delete reinterpret_cast<ApcCompatSession*>(handle);
502 }
503
504 const ApcCompatServiceHandle INVALID_SERVICE_HANDLE = nullptr;
505