1 /*
2  * Copyright 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 #ifndef ANDROID_HARDWARE_CONFIRMATIONUI_V1_0_TRUSTY_CONFIRMATIONUI_H
18 #define ANDROID_HARDWARE_CONFIRMATIONUI_V1_0_TRUSTY_CONFIRMATIONUI_H
19 
20 #include <aidl/android/hardware/confirmationui/BnConfirmationUI.h>
21 #include <aidl/android/hardware/confirmationui/IConfirmationResultCallback.h>
22 #include <aidl/android/hardware/confirmationui/UIOption.h>
23 #include <aidl/android/hardware/security/keymint/HardwareAuthToken.h>
24 #include <android/binder_manager.h>
25 
26 #include <atomic>
27 #include <condition_variable>
28 #include <memory>
29 #include <mutex>
30 #include <teeui/generic_messages.h>
31 #include <thread>
32 
33 #include "TrustyApp.h"
34 
35 namespace aidl::android::hardware::confirmationui {
36 
37 using std::shared_ptr;
38 using std::string;
39 using std::vector;
40 
41 using ::aidl::android::hardware::security::keymint::HardwareAuthToken;
42 using ::android::trusty::confirmationui::TrustyApp;
43 
44 class TrustyConfirmationUI : public BnConfirmationUI {
45   public:
46     TrustyConfirmationUI();
47     virtual ~TrustyConfirmationUI();
48     // Methods from ::aidl::android::hardware::confirmationui::IConfirmationUI
49     // follow.
50     ::ndk::ScopedAStatus
51     promptUserConfirmation(const shared_ptr<IConfirmationResultCallback>& resultCB,
52                            const vector<uint8_t>& promptText, const vector<uint8_t>& extraData,
53                            const string& locale, const vector<UIOption>& uiOptions) override;
54     ::ndk::ScopedAStatus
55     deliverSecureInputEvent(const HardwareAuthToken& secureInputToken) override;
56 
57     ::ndk::ScopedAStatus abort() override;
58 
59   private:
60     std::weak_ptr<TrustyApp> app_;
61     std::thread callback_thread_;
62 
63     enum class ListenerState : uint32_t {
64         None,
65         Starting,
66         SetupDone,
67         Interactive,
68         Terminating,
69     };
70 
71     /*
72      * listener_state is protected by listener_state_lock. It makes transitions between phases
73      * of the confirmation operation atomic.
74      * (See TrustyConfirmationUI.cpp#promptUserConfirmation_ for details about operation phases)
75      */
76     ListenerState listener_state_;
77     /*
78      * abort_called_ is also protected by listener_state_lock_ and indicates that the HAL user
79      * called abort.
80      */
81     bool abort_called_;
82     std::mutex listener_state_lock_;
83     std::condition_variable listener_state_condv_;
84     int prompt_result_;
85     bool secureInputDelivered_;
86 
87     std::tuple<teeui::ResponseCode, teeui::MsgVector<uint8_t>, teeui::MsgVector<uint8_t>>
88     promptUserConfirmation_(const teeui::MsgString& promptText,
89                             const teeui::MsgVector<uint8_t>& extraData,
90                             const teeui::MsgString& locale,
91                             const teeui::MsgVector<teeui::UIOption>& uiOptions);
92 };
93 
94 }  // namespace aidl::android::hardware::confirmationui
95 
96 #endif  // ANDROID_HARDWARE_CONFIRMATIONUI_V1_0_TRUSTY_CONFIRMATIONUI_H
97