1 /*
2  * Copyright 2019, 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 #pragma once
18 
19 #include <secure_input/secure_input_proto.h>
20 #include <stdint.h>
21 #include "trusty_time_stamper.h"
22 
23 #include <teeui/common_message_types.h>
24 
25 class InputTracker {
26 public:
27     enum class InputState : uint32_t {
28         None,
29         Fresh,
30         HandshakeOutstanding,
31         HandshakeComplete,
32         InputDeliveredFinal,
33         InputDeliveredMorePending,
34         InputFetched,
35         // insert new states above
36         Count,
37     };
38     enum class InputEvent : uint32_t {
39         None,
40         UserCancel,
41         UserConfirm,
42     };
43 
InputTracker()44     InputTracker() : state_(InputState::None), event_(InputEvent::None) {}
45     // new Session
46     teeui::ResponseCode newSession();
47 
48     // input Handshake
49     std::tuple<teeui::ResponseCode, secure_input::Nonce> beginHandshake();
50 
51     // input Handshake fianlize
52     teeui::ResponseCode finalizeHandshake(
53             const secure_input::Nonce& nCi,
54             const secure_input::Signature& signature,
55             const teeui::AuthTokenKey& key);
56 
57     // input event
58     std::tuple<teeui::ResponseCode, secure_input::InputResponse>
59     processInputEvent(secure_input::DTupKeyEvent keyEvent,
60                       const secure_input::Signature& signature,
61                       const teeui::AuthTokenKey& key);
62 
63     // fetch result
64     teeui::ResponseCode fetchInputEvent();
65 
66     teeui::ResponseCode reportVerifiedInput(InputEvent event);
abort()67     void abort() {
68         state_ = InputState::None;
69         event_ = InputEvent::None;
70     }
71 
72 private:
73     InputState state_;
74     InputEvent event_;
75     secure_input::Nonce input_nonce_;
76     monotonic_time_stamper::TimeStamp timestamps_[uint32_t(InputState::Count)];
77 };
78