blob: ffe7595ec5e6f84b2fd658cf83c38f538df34dd3 [file] [log] [blame]
Janis Danisevskis7a1cf382020-11-20 11:22:14 -08001/*
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 Nyamagouddc4b6122022-09-14 23:14:29 +000022#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 Danisevskis7a1cf382020-11-20 11:22:14 -080028#include <memory>
29#include <string>
30#include <thread>
31#include <vector>
32
33#define LOG_TAG "keystore2_apc_compat"
34
35namespace keystore2 {
36
37using android::sp;
38using android::hardware::hidl_death_recipient;
39using android::hardware::hidl_vec;
40using android::hardware::Return;
41using android::hardware::Status;
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +000042using HidlConfirmationResultCb =
43 android::hardware::confirmationui::V1_0::IConfirmationResultCallback;
44using HidlConfirmationUI = android::hardware::confirmationui::V1_0::IConfirmationUI;
Janis Danisevskis7a1cf382020-11-20 11:22:14 -080045using android::hardware::confirmationui::V1_0::ResponseCode;
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +000046using HidlUIOptions = android::hardware::confirmationui::V1_0::UIOption;
Janis Danisevskis7a1cf382020-11-20 11:22:14 -080047
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +000048using AidlConfirmationUI = ::aidl::android::hardware::confirmationui::IConfirmationUI;
49using AidlBnConfirmationResultCb =
50 ::aidl::android::hardware::confirmationui::BnConfirmationResultCallback;
51using AidlUIOptions = ::aidl::android::hardware::confirmationui::UIOption;
Janis Danisevskis7a1cf382020-11-20 11:22:14 -080052
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +000053class CompatSessionCB {
Janis Danisevskis7a1cf382020-11-20 11:22:14 -080054 public:
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +000055 void
56 finalize(uint32_t responseCode, ApcCompatCallback callback,
57 std::optional<std::reference_wrapper<const std::vector<uint8_t>>> dataConfirmed,
58 std::optional<std::reference_wrapper<const std::vector<uint8_t>>> confirmationToken) {
59 if (callback.result != nullptr) {
60 size_t dataConfirmedSize = 0;
61 const uint8_t* dataConfirmedPtr = nullptr;
62 size_t confirmationTokenSize = 0;
63 const uint8_t* confirmationTokenPtr = nullptr;
64 if (responseCode == APC_COMPAT_ERROR_OK) {
65 if (dataConfirmed) {
66 dataConfirmedPtr = dataConfirmed->get().data();
67 dataConfirmedSize = dataConfirmed->get().size();
68 }
69 if (confirmationToken) {
70 confirmationTokenPtr = confirmationToken->get().data();
71 confirmationTokenSize = confirmationToken->get().size();
72 }
73 }
74 callback.result(callback.data, responseCode, dataConfirmedPtr, dataConfirmedSize,
75 confirmationTokenPtr, confirmationTokenSize);
76 }
77 }
78};
79
80class ConfuiHidlCompatSession : public HidlConfirmationResultCb,
81 public hidl_death_recipient,
82 public CompatSessionCB {
83 public:
84 static sp<ConfuiHidlCompatSession> tryGetService() {
85 sp<HidlConfirmationUI> service = HidlConfirmationUI::tryGetService();
Janis Danisevskis7a1cf382020-11-20 11:22:14 -080086 if (service) {
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +000087 return sp<ConfuiHidlCompatSession>(new ConfuiHidlCompatSession(std::move(service)));
Janis Danisevskis7a1cf382020-11-20 11:22:14 -080088 } else {
89 return nullptr;
90 }
91 }
92
93 uint32_t promptUserConfirmation(ApcCompatCallback callback, const char* prompt_text,
94 const uint8_t* extra_data, size_t extra_data_size,
95 const char* locale, ApcCompatUiOptions ui_options) {
96 std::string hidl_prompt(prompt_text);
97 std::vector<uint8_t> hidl_extra(extra_data, extra_data + extra_data_size);
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +000098 std::vector<HidlUIOptions> hidl_ui_options;
Janis Danisevskis7a1cf382020-11-20 11:22:14 -080099 if (ui_options.inverted) {
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000100 hidl_ui_options.push_back(HidlUIOptions::AccessibilityInverted);
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800101 }
102 if (ui_options.magnified) {
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000103 hidl_ui_options.push_back(HidlUIOptions::AccessibilityMagnified);
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800104 }
105 auto lock = std::lock_guard(callback_lock_);
106 if (callback_.result != nullptr) {
107 return APC_COMPAT_ERROR_OPERATION_PENDING;
108 }
109 auto err = service_->linkToDeath(sp(this), 0);
110 if (!err.isOk()) {
111 LOG(ERROR) << "Communication error: promptUserConfirmation: "
112 "Trying to register death recipient: "
113 << err.description();
114 return APC_COMPAT_ERROR_SYSTEM_ERROR;
115 }
116
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000117 auto rc = service_->promptUserConfirmation(sp(this), hidl_prompt, hidl_extra, locale,
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800118 hidl_ui_options);
119 if (!rc.isOk()) {
120 LOG(ERROR) << "Communication error: promptUserConfirmation: " << rc.description();
Shaquille Johnson718036a2023-08-10 15:16:43 +0100121 } else if (rc == ResponseCode::OK) {
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800122 callback_ = callback;
123 }
124 return responseCode2Compat(rc.withDefault(ResponseCode::SystemError));
125 }
126
127 void abort() { service_->abort(); }
128
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000129 void finalize(ResponseCode responseCode, const hidl_vec<uint8_t>& dataConfirmed,
130 const hidl_vec<uint8_t>& confirmationToken) {
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800131 ApcCompatCallback callback;
132 {
133 auto lock = std::lock_guard(callback_lock_);
134 // Calling the callback consumes the callback data structure. We have to make
135 // sure that it can only be called once.
136 callback = callback_;
137 callback_ = {nullptr, nullptr};
138 // Unlock the callback_lock_ here. It must never be held while calling the callback.
139 }
140
141 if (callback.result != nullptr) {
142 service_->unlinkToDeath(sp(this));
143
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000144 std::vector<uint8_t> data = dataConfirmed;
145 std::vector<uint8_t> token = confirmationToken;
146
147 CompatSessionCB::finalize(responseCode2Compat(responseCode), callback, data, token);
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800148 }
149 }
150
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000151 // HidlConfirmationResultCb overrides:
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800152 android::hardware::Return<void> result(ResponseCode responseCode,
153 const hidl_vec<uint8_t>& dataConfirmed,
154 const hidl_vec<uint8_t>& confirmationToken) override {
155 finalize(responseCode, dataConfirmed, confirmationToken);
156 return Status::ok();
157 };
158
159 void serviceDied(uint64_t /* cookie */,
160 const ::android::wp<::android::hidl::base::V1_0::IBase>& /* who */) override {
161 finalize(ResponseCode::SystemError, {}, {});
162 }
163
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000164 static uint32_t responseCode2Compat(ResponseCode rc) {
165 switch (rc) {
166 case ResponseCode::OK:
167 return APC_COMPAT_ERROR_OK;
168 case ResponseCode::Canceled:
169 return APC_COMPAT_ERROR_CANCELLED;
170 case ResponseCode::Aborted:
171 return APC_COMPAT_ERROR_ABORTED;
172 case ResponseCode::OperationPending:
173 return APC_COMPAT_ERROR_OPERATION_PENDING;
174 case ResponseCode::Ignored:
175 return APC_COMPAT_ERROR_IGNORED;
176 case ResponseCode::SystemError:
177 case ResponseCode::Unimplemented:
178 case ResponseCode::Unexpected:
179 case ResponseCode::UIError:
180 case ResponseCode::UIErrorMissingGlyph:
181 case ResponseCode::UIErrorMessageTooLong:
182 case ResponseCode::UIErrorMalformedUTF8Encoding:
183 default:
184 return APC_COMPAT_ERROR_SYSTEM_ERROR;
185 }
186 }
187
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800188 private:
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000189 ConfuiHidlCompatSession(sp<HidlConfirmationUI> service)
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800190 : service_(service), callback_{nullptr, nullptr} {}
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000191 sp<HidlConfirmationUI> service_;
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800192
193 // The callback_lock_ protects the callback_ field against concurrent modification.
194 // IMPORTANT: It must never be held while calling the call back.
195 std::mutex callback_lock_;
196 ApcCompatCallback callback_;
197};
198
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000199class ConfuiAidlCompatSession : public AidlBnConfirmationResultCb, public CompatSessionCB {
200 public:
201 static std::shared_ptr<ConfuiAidlCompatSession> tryGetService() {
202 constexpr const char confirmationUIServiceName[] =
203 "android.hardware.confirmationui.IConfirmationUI/default";
204 if (!AServiceManager_isDeclared(confirmationUIServiceName)) {
205 LOG(INFO) << confirmationUIServiceName << " is not declared in VINTF";
206 return nullptr;
207 }
208 std::shared_ptr<AidlConfirmationUI> aidlService = AidlConfirmationUI::fromBinder(
209 ndk::SpAIBinder(AServiceManager_waitForService(confirmationUIServiceName)));
210 if (aidlService) {
211 return ::ndk::SharedRefBase::make<ConfuiAidlCompatSession>(aidlService);
212 }
213
214 return nullptr;
215 }
216
217 uint32_t promptUserConfirmation(ApcCompatCallback callback, const char* prompt_text,
218 const uint8_t* extra_data, size_t extra_data_size,
219 const char* locale, ApcCompatUiOptions ui_options) {
220 std::vector<uint8_t> aidl_prompt(prompt_text, prompt_text + strlen(prompt_text));
221 std::vector<uint8_t> aidl_extra(extra_data, extra_data + extra_data_size);
222 std::vector<AidlUIOptions> aidl_ui_options;
223 if (ui_options.inverted) {
224 aidl_ui_options.push_back(AidlUIOptions::ACCESSIBILITY_INVERTED);
225 }
226 if (ui_options.magnified) {
227 aidl_ui_options.push_back(AidlUIOptions::ACCESSIBILITY_MAGNIFIED);
228 }
229 auto lock = std::lock_guard(callback_lock_);
230 if (callback_.result != nullptr) {
231 return APC_COMPAT_ERROR_OPERATION_PENDING;
232 }
233
234 if (!aidlService_) {
235 return APC_COMPAT_ERROR_SYSTEM_ERROR;
236 }
237 auto linkRet =
238 AIBinder_linkToDeath(aidlService_->asBinder().get(), death_recipient_.get(), this);
239 if (linkRet != STATUS_OK) {
240 LOG(ERROR) << "Communication error: promptUserConfirmation: "
241 "Trying to register death recipient: ";
242 return APC_COMPAT_ERROR_SYSTEM_ERROR;
243 }
244
245 auto rc = aidlService_->promptUserConfirmation(ref<ConfuiAidlCompatSession>(), aidl_prompt,
246 aidl_extra, locale, aidl_ui_options);
247 int ret = getReturnCode(rc);
248 if (ret == AidlConfirmationUI::OK) {
249 callback_ = callback;
250 } else {
251 LOG(ERROR) << "Communication error: promptUserConfirmation: " << rc.getDescription();
252 }
253 return responseCode2Compat(ret);
254 }
255
256 void abort() {
257 if (aidlService_) {
258 aidlService_->abort();
259 }
260 }
261
262 void
263 finalize(int32_t responseCode,
264 std::optional<std::reference_wrapper<const std::vector<uint8_t>>> dataConfirmed,
265 std::optional<std::reference_wrapper<const std::vector<uint8_t>>> confirmationToken) {
266 ApcCompatCallback callback;
267 {
268 auto lock = std::lock_guard(callback_lock_);
269 // Calling the callback consumes the callback data structure. We have to make
270 // sure that it can only be called once.
271 callback = callback_;
272 callback_ = {nullptr, nullptr};
273 // Unlock the callback_lock_ here. It must never be held while calling the callback.
274 }
275
276 if (callback.result != nullptr) {
277 if (aidlService_) {
278 AIBinder_unlinkToDeath(aidlService_->asBinder().get(), death_recipient_.get(),
279 this);
280 }
281 CompatSessionCB::finalize(responseCode2Compat(responseCode), callback, dataConfirmed,
282 confirmationToken);
283 }
284 }
285
286 // AidlBnConfirmationResultCb overrides:
287 ::ndk::ScopedAStatus result(int32_t responseCode, const std::vector<uint8_t>& dataConfirmed,
288 const std::vector<uint8_t>& confirmationToken) override {
289 finalize(responseCode, dataConfirmed, confirmationToken);
290 return ::ndk::ScopedAStatus::ok();
291 };
292
293 void serviceDied() {
294 aidlService_.reset();
295 aidlService_ = nullptr;
296 finalize(AidlConfirmationUI::SYSTEM_ERROR, {}, {});
297 }
298
299 static void binderDiedCallbackAidl(void* ptr) {
300 LOG(ERROR) << __func__ << " : ConfuiAidlCompatSession Service died.";
301 auto aidlSession = static_cast<ConfuiAidlCompatSession*>(ptr);
302 if (aidlSession == nullptr) {
303 LOG(ERROR) << __func__ << ": Null ConfuiAidlCompatSession HAL died.";
304 return;
305 }
306 aidlSession->serviceDied();
307 }
308
309 int getReturnCode(const ::ndk::ScopedAStatus& result) {
310 if (result.isOk()) return AidlConfirmationUI::OK;
311
312 if (result.getExceptionCode() == EX_SERVICE_SPECIFIC) {
313 return static_cast<int>(result.getServiceSpecificError());
314 }
315 return result.getStatus();
316 }
317
318 uint32_t responseCode2Compat(int32_t rc) {
319 switch (rc) {
320 case AidlConfirmationUI::OK:
321 return APC_COMPAT_ERROR_OK;
322 case AidlConfirmationUI::CANCELED:
323 return APC_COMPAT_ERROR_CANCELLED;
324 case AidlConfirmationUI::ABORTED:
325 return APC_COMPAT_ERROR_ABORTED;
326 case AidlConfirmationUI::OPERATION_PENDING:
327 return APC_COMPAT_ERROR_OPERATION_PENDING;
328 case AidlConfirmationUI::IGNORED:
329 return APC_COMPAT_ERROR_IGNORED;
330 case AidlConfirmationUI::SYSTEM_ERROR:
331 case AidlConfirmationUI::UNIMPLEMENTED:
332 case AidlConfirmationUI::UNEXPECTED:
333 case AidlConfirmationUI::UI_ERROR:
334 case AidlConfirmationUI::UI_ERROR_MISSING_GLYPH:
335 case AidlConfirmationUI::UI_ERROR_MESSAGE_TOO_LONG:
336 case AidlConfirmationUI::UI_ERROR_MALFORMED_UTF8ENCODING:
337 default:
338 return APC_COMPAT_ERROR_SYSTEM_ERROR;
339 }
340 }
341
342 ConfuiAidlCompatSession(std::shared_ptr<AidlConfirmationUI> service)
343 : aidlService_(service), callback_{nullptr, nullptr} {
344 death_recipient_ = ::ndk::ScopedAIBinder_DeathRecipient(
345 AIBinder_DeathRecipient_new(binderDiedCallbackAidl));
346 }
347
348 virtual ~ConfuiAidlCompatSession() = default;
349 ConfuiAidlCompatSession(const ConfuiAidlCompatSession&) = delete;
350 ConfuiAidlCompatSession& operator=(const ConfuiAidlCompatSession&) = delete;
351
352 private:
353 std::shared_ptr<AidlConfirmationUI> aidlService_;
354
355 // The callback_lock_ protects the callback_ field against concurrent modification.
356 // IMPORTANT: It must never be held while calling the call back.
357 std::mutex callback_lock_;
358 ApcCompatCallback callback_;
359
360 ::ndk::ScopedAIBinder_DeathRecipient death_recipient_;
361};
362
363class ApcCompatSession {
364 public:
365 static ApcCompatServiceHandle getApcCompatSession() {
366 auto aidlCompatSession = ConfuiAidlCompatSession::tryGetService();
367 if (aidlCompatSession) {
368 return new ApcCompatSession(std::move(aidlCompatSession), nullptr);
369 }
370
371 sp<ConfuiHidlCompatSession> hidlCompatSession = ConfuiHidlCompatSession::tryGetService();
372 if (hidlCompatSession) {
373 return new ApcCompatSession(nullptr, std::move(hidlCompatSession));
374 }
375
376 LOG(ERROR) << "ConfirmationUI: Not found Service";
377 return nullptr;
378 }
379
380 uint32_t promptUserConfirmation(ApcCompatCallback callback, const char* prompt_text,
381 const uint8_t* extra_data, size_t extra_data_size,
382 char const* locale, ApcCompatUiOptions ui_options) {
383 if (aidlCompatSession_) {
384 return aidlCompatSession_->promptUserConfirmation(callback, prompt_text, extra_data,
385 extra_data_size, locale, ui_options);
386 } else {
387 return hidlCompatSession_->promptUserConfirmation(callback, prompt_text, extra_data,
388 extra_data_size, locale, ui_options);
389 }
390 }
391
392 void abortUserConfirmation() {
393 if (aidlCompatSession_) {
394 return aidlCompatSession_->abort();
395 } else {
396 return hidlCompatSession_->abort();
397 }
398 }
399
400 void closeUserConfirmationService() {
401 // Closing the handle implicitly aborts an ongoing sessions.
402 // Note that a resulting callback is still safely conducted, because we only delete a
403 // StrongPointer below. libhwbinder still owns another StrongPointer to this session.
404 abortUserConfirmation();
405 }
406
407 ApcCompatSession(std::shared_ptr<ConfuiAidlCompatSession> aidlCompatSession,
408 sp<ConfuiHidlCompatSession> hidlCompatSession)
409 : aidlCompatSession_(aidlCompatSession), hidlCompatSession_(hidlCompatSession) {}
410
411 private:
412 std::shared_ptr<ConfuiAidlCompatSession> aidlCompatSession_;
413 sp<ConfuiHidlCompatSession> hidlCompatSession_;
414};
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800415} // namespace keystore2
416
417using namespace keystore2;
418
419ApcCompatServiceHandle tryGetUserConfirmationService() {
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000420 return reinterpret_cast<ApcCompatServiceHandle>(ApcCompatSession::getApcCompatSession());
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800421}
422
423uint32_t promptUserConfirmation(ApcCompatServiceHandle handle, ApcCompatCallback callback,
424 const char* prompt_text, const uint8_t* extra_data,
425 size_t extra_data_size, char const* locale,
426 ApcCompatUiOptions ui_options) {
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000427 auto session = reinterpret_cast<ApcCompatSession*>(handle);
428 return session->promptUserConfirmation(callback, prompt_text, extra_data, extra_data_size,
429 locale, ui_options);
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800430}
431
432void abortUserConfirmation(ApcCompatServiceHandle handle) {
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000433 auto session = reinterpret_cast<ApcCompatSession*>(handle);
434 session->abortUserConfirmation();
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800435}
436
437void closeUserConfirmationService(ApcCompatServiceHandle handle) {
Rajesh Nyamagouddc4b6122022-09-14 23:14:29 +0000438 auto session = reinterpret_cast<ApcCompatSession*>(handle);
439 session->closeUserConfirmationService();
440 delete reinterpret_cast<ApcCompatSession*>(handle);
Janis Danisevskis7a1cf382020-11-20 11:22:14 -0800441}
442
443const ApcCompatServiceHandle INVALID_SERVICE_HANDLE = nullptr;