David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #define LOG_TAG "ConfirmationManager" |
| 18 | |
| 19 | #include "confirmation_manager.h" |
| 20 | |
| 21 | #include <android/hardware/confirmationui/1.0/IConfirmationResultCallback.h> |
| 22 | #include <android/hardware/confirmationui/1.0/IConfirmationUI.h> |
| 23 | #include <android/hardware/confirmationui/1.0/types.h> |
| 24 | #include <android/security/BpConfirmationPromptCallback.h> |
| 25 | #include <binder/BpBinder.h> |
| 26 | #include <binder/Parcel.h> |
| 27 | |
| 28 | #include "keystore_aidl_hidl_marshalling_utils.h" |
| 29 | |
| 30 | namespace keystore { |
| 31 | |
| 32 | using android::IBinder; |
| 33 | using android::sp; |
| 34 | using android::String16; |
| 35 | using android::String8; |
| 36 | using android::wp; |
| 37 | using android::binder::Status; |
| 38 | using android::hardware::hidl_vec; |
| 39 | using android::hardware::Return; |
| 40 | using android::hardware::confirmationui::V1_0::IConfirmationResultCallback; |
| 41 | using android::hardware::confirmationui::V1_0::IConfirmationUI; |
| 42 | using android::hardware::confirmationui::V1_0::UIOption; |
| 43 | |
| 44 | using android::security::BpConfirmationPromptCallback; |
| 45 | using std::lock_guard; |
| 46 | using std::mutex; |
| 47 | using std::vector; |
| 48 | |
| 49 | ConfirmationManager::ConfirmationManager(IBinder::DeathRecipient* deathRecipient) |
| 50 | : IConfirmationResultCallback(), mDeathRecipient(deathRecipient) {} |
| 51 | |
| 52 | // Called by keystore main thread. |
| 53 | Status ConfirmationManager::presentConfirmationPrompt(const sp<IBinder>& listener, |
| 54 | const String16& promptText, |
| 55 | const hidl_vec<uint8_t>& extraData, |
| 56 | const String16& locale, int uiOptionsAsFlags, |
| 57 | int32_t* aidl_return) { |
| 58 | lock_guard<mutex> lock(mMutex); |
| 59 | |
| 60 | if (mCurrentListener != nullptr) { |
| 61 | *aidl_return = static_cast<int32_t>(ConfirmationResponseCode::OperationPending); |
| 62 | return Status::ok(); |
| 63 | } |
| 64 | |
| 65 | sp<IConfirmationUI> confirmationUI = IConfirmationUI::tryGetService(); |
| 66 | if (confirmationUI == nullptr) { |
| 67 | ALOGW("Error getting confirmationUI service\n"); |
| 68 | *aidl_return = static_cast<int32_t>(ConfirmationResponseCode::Unimplemented); |
| 69 | return Status::ok(); |
| 70 | } |
| 71 | |
| 72 | String8 promptText8(promptText); |
| 73 | String8 locale8(locale); |
| 74 | vector<UIOption> uiOptionsVector; |
| 75 | for (int n = 0; n < 32; n++) { |
| 76 | if (uiOptionsAsFlags & (1 << n)) { |
| 77 | uiOptionsVector.push_back(UIOption(n)); |
| 78 | } |
| 79 | } |
| 80 | ConfirmationResponseCode responseCode; |
| 81 | responseCode = confirmationUI->promptUserConfirmation(sp<IConfirmationResultCallback>(this), |
| 82 | promptText8.string(), extraData, |
| 83 | locale8.string(), uiOptionsVector); |
| 84 | if (responseCode != ConfirmationResponseCode::OK) { |
| 85 | ALOGW("Unexpecxted responseCode %d from promptUserConfirmation\n", responseCode); |
| 86 | *aidl_return = static_cast<int32_t>(responseCode); |
| 87 | return Status::ok(); |
| 88 | } |
| 89 | |
| 90 | listener->linkToDeath(mDeathRecipient); |
| 91 | confirmationUI->linkToDeath(this, 0); |
| 92 | mCurrentListener = listener; |
| 93 | mCurrentConfirmationUI = confirmationUI; |
| 94 | |
| 95 | *aidl_return = static_cast<int32_t>(ConfirmationResponseCode::OK); |
| 96 | return Status::ok(); |
| 97 | } |
| 98 | |
| 99 | // Called by keystore main thread. |
| 100 | Status ConfirmationManager::cancelConfirmationPrompt(const sp<IBinder>& listener, |
| 101 | int32_t* aidl_return) { |
| 102 | mMutex.lock(); |
| 103 | if (mCurrentListener != listener) { |
| 104 | // If the prompt was displayed by another application, return |
| 105 | // OperationPending. |
| 106 | mMutex.unlock(); |
| 107 | *aidl_return = static_cast<int32_t>(ConfirmationResponseCode::OperationPending); |
| 108 | return Status::ok(); |
| 109 | } |
| 110 | mMutex.unlock(); |
| 111 | |
| 112 | finalizeTransaction(ConfirmationResponseCode::Aborted, {}, true); |
| 113 | |
| 114 | *aidl_return = static_cast<int32_t>(ConfirmationResponseCode::OK); |
| 115 | return Status::ok(); |
| 116 | } |
| 117 | |
| 118 | void ConfirmationManager::finalizeTransaction(ConfirmationResponseCode responseCode, |
| 119 | hidl_vec<uint8_t> dataThatWasConfirmed, |
| 120 | bool callAbortOnHal) { |
| 121 | // Note that confirmationUI->abort() may make the remote HAL process do an IPC call back |
| 122 | // into our process resulting in confirmationResultCallback() to be called... this in turn |
| 123 | // calls finalizeTransaction(). So we have to be careful a) not holding any locks; |
| 124 | // and b) ensure state has been cleared; before doing this... |
| 125 | |
| 126 | mMutex.lock(); |
| 127 | sp<IBinder> listener = mCurrentListener; |
| 128 | if (mCurrentListener != nullptr) { |
| 129 | mCurrentListener->unlinkToDeath(mDeathRecipient); |
| 130 | mCurrentListener = nullptr; |
| 131 | } |
| 132 | sp<IConfirmationUI> confirmationUI = mCurrentConfirmationUI; |
| 133 | if (mCurrentConfirmationUI != nullptr) { |
| 134 | mCurrentConfirmationUI->unlinkToDeath(this); |
| 135 | mCurrentConfirmationUI = nullptr; |
| 136 | } |
| 137 | mMutex.unlock(); |
| 138 | |
| 139 | // Tell the HAL to shut down the confirmation dialog, if requested. |
| 140 | if (confirmationUI != nullptr && callAbortOnHal) { |
| 141 | confirmationUI->abort(); |
| 142 | } |
| 143 | |
| 144 | // Deliver result to the application that started the operation. |
| 145 | if (listener != nullptr) { |
| 146 | sp<BpConfirmationPromptCallback> obj = new BpConfirmationPromptCallback(listener); |
| 147 | Status status = obj->onConfirmationPromptCompleted(static_cast<int32_t>(responseCode), |
| 148 | dataThatWasConfirmed); |
| 149 | if (!status.isOk()) { |
| 150 | ALOGW("Error sending onConfirmationPromptCompleted - status: %s\n", |
| 151 | status.toString8().c_str()); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Called by hwbinder thread (not keystore main thread). |
| 157 | Return<void> ConfirmationManager::result(ConfirmationResponseCode responseCode, |
| 158 | const hidl_vec<uint8_t>& dataThatWasConfirmed, |
| 159 | const hidl_vec<uint8_t>& confirmationToken) { |
| 160 | finalizeTransaction(responseCode, dataThatWasConfirmed, false); |
| 161 | lock_guard<mutex> lock(mMutex); |
| 162 | mLatestConfirmationToken = confirmationToken; |
| 163 | return Return<void>(); |
| 164 | } |
| 165 | |
| 166 | // Called by keystore main thread. |
| 167 | hidl_vec<uint8_t> ConfirmationManager::getLatestConfirmationToken() { |
| 168 | lock_guard<mutex> lock(mMutex); |
| 169 | return mLatestConfirmationToken; |
| 170 | } |
| 171 | |
| 172 | void ConfirmationManager::binderDied(const wp<IBinder>& who) { |
| 173 | // This is also called for other binders so need to check it's for |
| 174 | // us before acting on it. |
| 175 | mMutex.lock(); |
| 176 | if (who == mCurrentListener) { |
| 177 | // Clear this so we don't call back into the already dead |
| 178 | // binder in finalizeTransaction(). |
| 179 | mCurrentListener->unlinkToDeath(mDeathRecipient); |
| 180 | mCurrentListener = nullptr; |
| 181 | mMutex.unlock(); |
| 182 | ALOGW("The process which requested the confirmation dialog died.\n"); |
| 183 | finalizeTransaction(ConfirmationResponseCode::SystemError, {}, true); |
| 184 | } else { |
| 185 | mMutex.unlock(); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void ConfirmationManager::serviceDied(uint64_t /* cookie */, |
| 190 | const wp<android::hidl::base::V1_0::IBase>& /* who */) { |
| 191 | ALOGW("The ConfirmationUI HAL died.\n"); |
| 192 | finalizeTransaction(ConfirmationResponseCode::SystemError, {}, false); |
| 193 | } |
| 194 | |
| 195 | } // namespace keystore |