Upstream version 9.38.198.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 backed token for the system
27 // slot when the user logs in. It is expected to be constructed on the UI thread
28 // and public methods should all be called from the UI thread.
29 // When the TPM token is loaded, or if the TPM should stay disabled for the
30 // session, the observers are notified using |OnTPMTokenReady|.
31 // Note: This currently initializes the token with the hard coded default id 0.
32 // See CryptohomeClient::OnPkcs11GetTpmTokenInfo.
33 class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer {
34  public:
35   class Observer {
36    public:
37     // Called when the TPM token initialization is done or the case where TPM
38     // should stay disabled is detected (e.g. on guest login).
39     virtual void OnTPMTokenReady() = 0;
40
41    protected:
42     virtual ~Observer() {}
43   };
44
45   // Sets the global instance. Must be called before any calls to Get().
46   // The global instance will immediately start observing |LoginState|.
47   static void Initialize();
48
49   // Sets the global. stubbed out, instance. To be used in tests.
50   static void InitializeForTest();
51
52   // Destroys the global instance.
53   static void Shutdown();
54
55   // Gets the global instance. Initialize() must be called before this.
56   static TPMTokenLoader* Get();
57
58   // Returns true if the global instance has been initialized.
59   static bool IsInitialized();
60
61   // |crypto_task_runner| is the task runner that any synchronous crypto calls
62   // should be made from, e.g. in Chrome this is the IO thread. Must be called
63   // after the thread is started. When called, this will attempt to start TPM
64   // token loading.
65   void SetCryptoTaskRunner(
66       const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner);
67
68   void AddObserver(TPMTokenLoader::Observer* observer);
69   void RemoveObserver(TPMTokenLoader::Observer* observer);
70
71   // Checks if the TPM token in ready to be used.
72   bool IsTPMTokenReady() const;
73
74   std::string tpm_user_pin() const { return tpm_user_pin_; }
75
76  private:
77   explicit TPMTokenLoader(bool for_test);
78   virtual ~TPMTokenLoader();
79
80   // Starts tpm token initialization if the user is logged in and the crypto
81   // task runner is set.
82   void MaybeStartTokenInitialization();
83
84   // This is the cyclic chain of callbacks to initialize the TPM token.
85   void ContinueTokenInitialization();
86   void OnTPMTokenEnabledForNSS();
87   void OnTpmIsEnabled(DBusMethodCallStatus call_status,
88                       bool tpm_is_enabled);
89   void OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status,
90                                bool is_tpm_token_ready);
91   void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status,
92                                const std::string& token_name,
93                                const std::string& user_pin,
94                                int token_slot_id);
95   void OnTPMTokenInitialized(bool success);
96
97   // If token initialization step fails (e.g. if tpm token is not yet ready)
98   // schedules the initialization step retry attempt after a timeout.
99   void RetryTokenInitializationLater();
100
101   // Notifies observers that the TPM token is ready.
102   void NotifyTPMTokenReady();
103
104   // LoginState::Observer
105   virtual void LoggedInStateChanged() OVERRIDE;
106
107   bool initialized_for_test_;
108
109   ObserverList<Observer> observers_;
110
111   // The states are traversed in this order but some might get omitted or never
112   // be left.
113   enum TPMTokenState {
114     TPM_STATE_UNKNOWN,
115     TPM_INITIALIZATION_STARTED,
116     TPM_TOKEN_ENABLED_FOR_NSS,
117     TPM_DISABLED,
118     TPM_ENABLED,
119     TPM_TOKEN_READY,
120     TPM_TOKEN_INFO_RECEIVED,
121     TPM_TOKEN_INITIALIZED,
122   };
123   TPMTokenState tpm_token_state_;
124
125   // The current request delay before the next attempt to initialize the
126   // TPM. Will be adapted after each attempt.
127   base::TimeDelta tpm_request_delay_;
128
129   // Cached TPM token info.
130   int tpm_token_slot_id_;
131   std::string tpm_user_pin_;
132
133   base::ThreadChecker thread_checker_;
134
135   // TaskRunner for crypto calls.
136   scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_;
137
138   base::WeakPtrFactory<TPMTokenLoader> weak_factory_;
139
140   DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader);
141 };
142
143 }  // namespace chromeos
144
145 #endif  // CHROMEOS_TPM_TOKEN_LOADER_H_