OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROMEOS_TPM_TOKEN_LOADER_H_ | 5 #ifndef CHROMEOS_TPM_TOKEN_LOADER_H_ |
6 #define CHROMEOS_TPM_TOKEN_LOADER_H_ | 6 #define CHROMEOS_TPM_TOKEN_LOADER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 // This class is responsible for loading the TPM backed token for the system | 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 | 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. | 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 | 29 // When the TPM token is loaded, or if the TPM should stay disabled for the |
30 // session, the observers are notified using |OnTPMTokenReady|. | 30 // session, the observers are notified using |OnTPMTokenReady|. |
31 // Note: This currently initializes the token with the hard coded default id 0. | 31 // Note: This currently initializes the token with the hard coded default id 0. |
32 // See CryptohomeClient::OnPkcs11GetTpmTokenInfo. | 32 // See CryptohomeClient::OnPkcs11GetTpmTokenInfo. |
33 class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer { | 33 class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer { |
34 public: | 34 public: |
35 class Observer { | 35 enum TPMTokenStatus { |
36 public: | 36 TPM_TOKEN_STATUS_UNDETERMINED, |
37 // Called when the TPM token initialization is done or the case where TPM | 37 TPM_TOKEN_STATUS_ENABLED, |
38 // should stay disabled is detected (e.g. on guest login). | 38 TPM_TOKEN_STATUS_DISABLED |
39 virtual void OnTPMTokenReady() = 0; | 39 }; |
40 | 40 |
41 protected: | 41 typedef base::Callback<void(bool)> TPMReadyCallback; |
42 virtual ~Observer() {} | 42 typedef std::vector<TPMReadyCallback> TPMReadyCallbackList; |
43 }; | |
44 | 43 |
45 // Sets the global instance. Must be called before any calls to Get(). | 44 // Sets the global instance. Must be called before any calls to Get(). |
46 // The global instance will immediately start observing |LoginState|. | 45 // The global instance will immediately start observing |LoginState|. |
47 static void Initialize(); | 46 static void Initialize(); |
48 | 47 |
49 // Sets the global. stubbed out, instance. To be used in tests. | 48 // Sets the global. stubbed out, instance. To be used in tests. |
50 static void InitializeForTest(); | 49 static void InitializeForTest(); |
51 | 50 |
52 // Destroys the global instance. | 51 // Destroys the global instance. |
53 static void Shutdown(); | 52 static void Shutdown(); |
54 | 53 |
55 // Gets the global instance. Initialize() must be called before this. | 54 // Gets the global instance. Initialize() must be called before this. |
56 static TPMTokenLoader* Get(); | 55 static TPMTokenLoader* Get(); |
57 | 56 |
58 // Returns true if the global instance has been initialized. | 57 // Returns true if the global instance has been initialized. |
59 static bool IsInitialized(); | 58 static bool IsInitialized(); |
60 | 59 |
61 // |crypto_task_runner| is the task runner that any synchronous crypto calls | 60 // |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 | 61 // 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 | 62 // after the thread is started. When called, this will attempt to start TPM |
64 // token loading. | 63 // token loading. |
65 void SetCryptoTaskRunner( | 64 void SetCryptoTaskRunner( |
66 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner); | 65 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner); |
67 | 66 |
68 void AddObserver(TPMTokenLoader::Observer* observer); | 67 // Checks if the TPM token is enabled. If the state is unknown, |callback| |
69 void RemoveObserver(TPMTokenLoader::Observer* observer); | 68 // will be called back once the TPM state is known. |
70 | 69 TPMTokenStatus IsTPMTokenEnabled(const TPMReadyCallback& callback); |
71 // Checks if the TPM token in ready to be used. | |
72 bool IsTPMTokenReady() const; | |
73 | 70 |
74 std::string tpm_user_pin() const { return tpm_user_pin_; } | 71 std::string tpm_user_pin() const { return tpm_user_pin_; } |
75 | 72 |
76 private: | 73 private: |
77 explicit TPMTokenLoader(bool for_test); | 74 explicit TPMTokenLoader(bool for_test); |
78 virtual ~TPMTokenLoader(); | 75 virtual ~TPMTokenLoader(); |
79 | 76 |
| 77 bool IsTPMLoadingEnabled() const; |
| 78 |
80 // Starts tpm token initialization if the user is logged in and the crypto | 79 // Starts tpm token initialization if the user is logged in and the crypto |
81 // task runner is set. | 80 // task runner is set. |
82 void MaybeStartTokenInitialization(); | 81 void MaybeStartTokenInitialization(); |
83 | 82 |
84 // This is the cyclic chain of callbacks to initialize the TPM token. | 83 // This is the cyclic chain of callbacks to initialize the TPM token. |
85 void ContinueTokenInitialization(); | 84 void ContinueTokenInitialization(); |
86 void OnTPMTokenEnabledForNSS(); | 85 void OnTPMTokenEnabledForNSS(); |
87 void OnTpmIsEnabled(DBusMethodCallStatus call_status, | 86 void OnTpmIsEnabled(DBusMethodCallStatus call_status, |
88 bool tpm_is_enabled); | 87 bool tpm_is_enabled); |
89 void OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, | 88 void OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, |
90 bool is_tpm_token_ready); | 89 bool is_tpm_token_ready); |
91 void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status, | 90 void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status, |
92 const std::string& token_name, | 91 const std::string& token_name, |
93 const std::string& user_pin, | 92 const std::string& user_pin, |
94 int token_slot_id); | 93 int token_slot_id); |
95 void OnTPMTokenInitialized(bool success); | 94 void OnTPMTokenInitialized(bool success); |
96 | 95 |
97 // If token initialization step fails (e.g. if tpm token is not yet ready) | 96 // If token initialization step fails (e.g. if tpm token is not yet ready) |
98 // schedules the initialization step retry attempt after a timeout. | 97 // schedules the initialization step retry attempt after a timeout. |
99 void RetryTokenInitializationLater(); | 98 void RetryTokenInitializationLater(); |
100 | 99 |
101 // Notifies observers that the TPM token is ready. | 100 // Notifies observers that the TPM token is ready. |
102 void NotifyTPMTokenReady(); | 101 void NotifyTPMTokenReady(); |
103 | 102 |
104 // LoginState::Observer | 103 // LoginState::Observer |
105 virtual void LoggedInStateChanged() OVERRIDE; | 104 virtual void LoggedInStateChanged() OVERRIDE; |
106 | 105 |
107 bool initialized_for_test_; | 106 bool initialized_for_test_; |
108 | 107 |
109 ObserverList<Observer> observers_; | 108 TPMReadyCallbackList tpm_ready_callback_list_; |
110 | 109 |
111 // The states are traversed in this order but some might get omitted or never | 110 // The states are traversed in this order but some might get omitted or never |
112 // be left. | 111 // be left. |
113 enum TPMTokenState { | 112 enum TPMTokenState { |
114 TPM_STATE_UNKNOWN, | 113 TPM_STATE_UNKNOWN, |
115 TPM_INITIALIZATION_STARTED, | 114 TPM_INITIALIZATION_STARTED, |
116 TPM_TOKEN_ENABLED_FOR_NSS, | 115 TPM_TOKEN_ENABLED_FOR_NSS, |
117 TPM_DISABLED, | 116 TPM_DISABLED, |
118 TPM_ENABLED, | 117 TPM_ENABLED, |
119 TPM_TOKEN_READY, | 118 TPM_TOKEN_READY, |
(...skipping 16 matching lines...) Expand all Loading... |
136 scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_; | 135 scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_; |
137 | 136 |
138 base::WeakPtrFactory<TPMTokenLoader> weak_factory_; | 137 base::WeakPtrFactory<TPMTokenLoader> weak_factory_; |
139 | 138 |
140 DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader); | 139 DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader); |
141 }; | 140 }; |
142 | 141 |
143 } // namespace chromeos | 142 } // namespace chromeos |
144 | 143 |
145 #endif // CHROMEOS_TPM_TOKEN_LOADER_H_ | 144 #endif // CHROMEOS_TPM_TOKEN_LOADER_H_ |
OLD | NEW |