Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chromeos / tpm_token_loader.h
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROMEOS_TPM_TOKEN_LOADER_H_
6 #define CHROMEOS_TPM_TOKEN_LOADER_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/threading/thread_checker.h"
15 #include "base/time/time.h"
16 #include "chromeos/chromeos_export.h"
17 #include "chromeos/dbus/dbus_method_call_status.h"
18 #include "chromeos/login/login_state.h"
19
20 namespace base {
21 class SequencedTaskRunner;
22 }
23
24 namespace chromeos {
25
26 // This class is responsible for loading the TPM token when the user logs
27 // in. It is expected to be constructed on the UI thread and public methods
28 // should all be called from the UI thread. When the TPM token is loaded,
29 // or if the TPM should stay disabled for the session, the observers are
30 // notified using |OnTPMTokenReady|.
31 class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer {
32  public:
33   class Observer {
34    public:
35     // Called when the TPM token initialization is done or the case where TPM
36     // should stay disabled is detected (e.g. on guest login). If TPM is
37     // disabled, |tpm_user_pin|, |tpm_token_name| and |tpm_token_slot_id| will
38     // not be set.
39     virtual void OnTPMTokenReady(const std::string& tpm_user_pin,
40                                  const std::string& tpm_token_name,
41                                  int tpm_token_slot_id) = 0;
42
43    protected:
44     virtual ~Observer() {}
45   };
46
47   // Sets the global instance. Must be called before any calls to Get().
48   // The global instance will immediately start observing |LoginState|.
49   static void Initialize();
50
51   // Destroys the global instance.
52   static void Shutdown();
53
54   // Gets the global instance. Initialize() must be called before this.
55   static TPMTokenLoader* Get();
56
57   // Returns true if the global instance has been initialized.
58   static bool IsInitialized();
59
60   // By default, TPMTokenLoader tries to load the TPMToken only if running
61   // in a ChromeOS environment. Tests can call this function after Initialize()
62   // and before SetCryptoTaskRunner() to enable the TPM initialization.
63   void InitializeTPMForTest();
64
65   // |crypto_task_runner| is the task runner that any synchronous crypto calls
66   // should be made from, e.g. in Chrome this is the IO thread. Must be called
67   // after the thread is started. When called, this will attempt to start TPM
68   // token loading.
69   void SetCryptoTaskRunner(
70       const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner);
71
72   void AddObserver(TPMTokenLoader::Observer* observer);
73   void RemoveObserver(TPMTokenLoader::Observer* observer);
74
75   // Checks if the TPM token in ready to be used.
76   bool IsTPMTokenReady() const;
77
78  private:
79   TPMTokenLoader();
80   virtual ~TPMTokenLoader();
81
82   // Starts tpm token initialization if the user is logged in and the crypto
83   // task runner is set.
84   void MaybeStartTokenInitialization();
85
86   // This is the cyclic chain of callbacks to initialize the TPM token.
87   void ContinueTokenInitialization();
88   void OnPersistentNSSDBOpened();
89   void OnTpmIsEnabled(DBusMethodCallStatus call_status,
90                       bool tpm_is_enabled);
91   void OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status,
92                                bool is_tpm_token_ready);
93   void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status,
94                                const std::string& token_name,
95                                const std::string& user_pin,
96                                int token_slot_id);
97   void OnTPMTokenInitialized(bool success);
98
99   // If token initialization step fails (e.g. if tpm token is not yet ready)
100   // schedules the initialization step retry attempt after a timeout.
101   void RetryTokenInitializationLater();
102
103   // Notifies observers that the TPM token is ready.
104   void NotifyTPMTokenReady();
105
106   // LoginState::Observer
107   virtual void LoggedInStateChanged() OVERRIDE;
108
109   bool initialize_tpm_for_test_;
110
111   ObserverList<Observer> observers_;
112
113   // The states are traversed in this order but some might get omitted or never
114   // be left.
115   enum TPMTokenState {
116     TPM_STATE_UNKNOWN,
117     TPM_INITIALIZATION_STARTED,
118     TPM_DB_OPENED,
119     TPM_DISABLED,
120     TPM_ENABLED,
121     TPM_TOKEN_READY,
122     TPM_TOKEN_INFO_RECEIVED,
123     TPM_TOKEN_INITIALIZED,
124   };
125   TPMTokenState tpm_token_state_;
126
127   // The current request delay before the next attempt to initialize the
128   // TPM. Will be adapted after each attempt.
129   base::TimeDelta tpm_request_delay_;
130
131   // Cached TPM token info.
132   std::string tpm_token_name_;
133   int tpm_token_slot_id_;
134   std::string tpm_user_pin_;
135
136   base::ThreadChecker thread_checker_;
137
138   // TaskRunner for crypto calls.
139   scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_;
140
141   base::WeakPtrFactory<TPMTokenLoader> weak_factory_;
142
143   DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader);
144 };
145
146 }  // namespace chromeos
147
148 #endif  // CHROMEOS_TPM_TOKEN_LOADER_H_