Add support for confirmation APIs.

This code implements new keystore APIs for confirmations.

Also add new 'confirmation' verb to the keystore_cli_v2 command to be
used for testing confirmations. It will block until there's a
callback. Example invocations:

 phone:/ # keystore_cli_v2 confirmation --prompt_text="Hello World" --extra_data=010203 --ui_options=1,2,3
 Waiting for prompt to complete - use Ctrl+C to abort...
 Confirmation prompt completed
 responseCode = 0
 dataThatWasConfirmed[30] = {0xa2, 0x66, 0x70, 0x72, 0x6f, 0x6d, 0x70, 0x74, 0x6b, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x65, 0x65, 0x78, 0x74, 0x72, 0x61, 0x43, 0x01, 0x02, 0x03}
 phone:/ #

If a prompt is already being shown, the |OperationPending| return code
(code 3) is returned:

 phone:/ # keystore_cli_v2 confirmation --prompt_text="Hello World" --extra_data=010203 --ui_options=1,2,3
 Presenting confirmation prompt failed with return code 3.

Canceling a prompt:

 phone:/# keystore_cli_v2 confirmation --prompt_text="Hello World" --extra_data=010203 --cancel_after=1.5
 Sleeping 1.5 seconds before canceling prompt...
 Waiting for prompt to complete - use Ctrl+C to abort...
 Confirmation prompt completed
 responseCode = 2
 dataThatWasConfirmed[0] = {}

Bug: 63928580
Test: Manually tested.
Change-Id: Ida14706ad066d5350b9081eb7821c7b1a1472dd2
diff --git a/keystore/confirmation_manager.h b/keystore/confirmation_manager.h
new file mode 100644
index 0000000..4bf4b8d
--- /dev/null
+++ b/keystore/confirmation_manager.h
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef KEYSTORE_CONFIRMATION_MANAGER_H_
+#define KEYSTORE_CONFIRMATION_MANAGER_H_
+
+#include <android/hardware/confirmationui/1.0/IConfirmationUI.h>
+#include <android/hardware/confirmationui/1.0/types.h>
+#include <binder/Binder.h>
+#include <binder/IBinder.h>
+#include <binder/Status.h>
+#include <keystore/keymaster_types.h>
+#include <map>
+#include <mutex>
+#include <utils/LruCache.h>
+#include <utils/StrongPointer.h>
+#include <vector>
+
+namespace keystore {
+
+using android::binder::Status;
+using android::hardware::confirmationui::V1_0::IConfirmationResultCallback;
+using ConfirmationResponseCode = android::hardware::confirmationui::V1_0::ResponseCode;
+
+class ConfirmationManager;
+
+class ConfirmationManager : public android::hardware::hidl_death_recipient,
+                            public IConfirmationResultCallback {
+  public:
+    explicit ConfirmationManager(android::IBinder::DeathRecipient* deathRecipient);
+
+    // Calls into the confirmationui HAL to start a new prompt.
+    //
+    // Returns OperationPending if another application is already
+    // showing a confirmation. Otherwise returns the return code from
+    // the HAL.
+    Status presentConfirmationPrompt(const android::sp<android::IBinder>& listener,
+                                     const android::String16& promptText,
+                                     const hidl_vec<uint8_t>& extraData,
+                                     const android::String16& locale, int uiOptionsAsFlags,
+                                     int32_t* aidl_return);
+
+    // Calls into the confirmationui HAL to cancel displaying a
+    // prompt.
+    //
+    // Returns OperatingPending if another application is showing a
+    // confirmation. Otherwise returns the return code from the HAL.
+    Status cancelConfirmationPrompt(const android::sp<android::IBinder>& listener,
+                                    int32_t* aidl_return);
+
+    // Gets the latest confirmation token received from the ConfirmationUI HAL.
+    hidl_vec<uint8_t> getLatestConfirmationToken();
+
+    // Called by KeyStoreService when a client binder has died.
+    void binderDied(const android::wp<android::IBinder>& who);
+
+    // hidl_death_recipient overrides:
+    virtual void serviceDied(uint64_t cookie,
+                             const android::wp<android::hidl::base::V1_0::IBase>& who) override;
+
+    // IConfirmationResultCallback overrides:
+    android::hardware::Return<void> result(ConfirmationResponseCode responseCode,
+                                           const hidl_vec<uint8_t>& dataThatWasConfirmed,
+                                           const hidl_vec<uint8_t>& confirmationToken) override;
+
+  private:
+    friend class ConfirmationResultCallback;
+
+    void finalizeTransaction(ConfirmationResponseCode responseCode,
+                             hidl_vec<uint8_t> dataThatWasConfirmed, bool callAbortOnHal);
+
+    // This mutex protects all data below it.
+    std::mutex mMutex;
+
+    // The mCurrentListener and mCurrentConfirmationUI fields are set
+    // if and only if a prompt is currently showing.
+    android::sp<android::IBinder> mCurrentListener;
+    android::sp<android::hardware::confirmationui::V1_0::IConfirmationUI> mCurrentConfirmationUI;
+    android::IBinder::DeathRecipient* mDeathRecipient;
+    hidl_vec<uint8_t> mLatestConfirmationToken;
+};
+
+}  // namespace keystore
+
+#endif  // KEYSTORE_CONFIRMATION_MANAGER_H_