Update .DEPS.git
[chromium-blink-merge.git] / chromeos / tpm_token_loader.h
blob8500d4a2d0e4b8bb999c49dd780196e5db0985b7
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.
5 #ifndef CHROMEOS_TPM_TOKEN_LOADER_H_
6 #define CHROMEOS_TPM_TOKEN_LOADER_H_
8 #include <string>
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"
20 namespace base {
21 class SequencedTaskRunner;
24 namespace chromeos {
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;
43 protected:
44 virtual ~Observer() {}
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();
51 // Destroys the global instance.
52 static void Shutdown();
54 // Gets the global instance. Initialize() must be called before this.
55 static TPMTokenLoader* Get();
57 // Returns true if the global instance has been initialized.
58 static bool IsInitialized();
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();
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);
72 void AddObserver(TPMTokenLoader::Observer* observer);
73 void RemoveObserver(TPMTokenLoader::Observer* observer);
75 // Checks if the TPM token in ready to be used.
76 bool IsTPMTokenReady() const;
78 private:
79 TPMTokenLoader();
80 virtual ~TPMTokenLoader();
82 // Starts tpm token initialization if the user is logged in and the crypto
83 // task runner is set.
84 void MaybeStartTokenInitialization();
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);
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();
103 // Notifies observers that the TPM token is ready.
104 void NotifyTPMTokenReady();
106 // LoginState::Observer
107 virtual void LoggedInStateChanged() OVERRIDE;
109 bool initialize_tpm_for_test_;
111 ObserverList<Observer> observers_;
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,
125 TPMTokenState tpm_token_state_;
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_;
131 // Cached TPM token info.
132 std::string tpm_token_name_;
133 int tpm_token_slot_id_;
134 std::string tpm_user_pin_;
136 base::ThreadChecker thread_checker_;
138 // TaskRunner for crypto calls.
139 scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_;
141 base::WeakPtrFactory<TPMTokenLoader> weak_factory_;
143 DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader);
146 } // namespace chromeos
148 #endif // CHROMEOS_TPM_TOKEN_LOADER_H_