Adding yfriedman@ as owner for the enhanced_bookmarks component.
[chromium-blink-merge.git] / chromeos / tpm_token_loader.h
blob16e20ef12e98d7054e172c0cdd9547f52ffada9e
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 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;
41 protected:
42 virtual ~Observer() {}
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();
49 // Sets the global. stubbed out, instance. To be used in tests.
50 static void InitializeForTest();
52 // Destroys the global instance.
53 static void Shutdown();
55 // Gets the global instance. Initialize() must be called before this.
56 static TPMTokenLoader* Get();
58 // Returns true if the global instance has been initialized.
59 static bool IsInitialized();
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);
68 void AddObserver(TPMTokenLoader::Observer* observer);
69 void RemoveObserver(TPMTokenLoader::Observer* observer);
71 // Checks if the TPM token in ready to be used.
72 bool IsTPMTokenReady() const;
74 std::string tpm_user_pin() const { return tpm_user_pin_; }
76 private:
77 explicit TPMTokenLoader(bool for_test);
78 virtual ~TPMTokenLoader();
80 // Starts tpm token initialization if the user is logged in and the crypto
81 // task runner is set.
82 void MaybeStartTokenInitialization();
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);
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();
101 // Notifies observers that the TPM token is ready.
102 void NotifyTPMTokenReady();
104 // LoginState::Observer
105 virtual void LoggedInStateChanged() OVERRIDE;
107 bool initialized_for_test_;
109 ObserverList<Observer> observers_;
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,
123 TPMTokenState tpm_token_state_;
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_;
129 // Cached TPM token info.
130 int tpm_token_slot_id_;
131 std::string tpm_user_pin_;
133 base::ThreadChecker thread_checker_;
135 // TaskRunner for crypto calls.
136 scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_;
138 base::WeakPtrFactory<TPMTokenLoader> weak_factory_;
140 DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader);
143 } // namespace chromeos
145 #endif // CHROMEOS_TPM_TOKEN_LOADER_H_