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 #include "chromeos/tpm_token_loader.h" | 5 #include "chromeos/tpm_token_loader.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 bool success) { | 43 bool success) { |
44 runner->PostTask(FROM_HERE, base::Bind(callback, success)); | 44 runner->PostTask(FROM_HERE, base::Bind(callback, success)); |
45 } | 45 } |
46 | 46 |
47 } // namespace | 47 } // namespace |
48 | 48 |
49 static TPMTokenLoader* g_tpm_token_loader = NULL; | 49 static TPMTokenLoader* g_tpm_token_loader = NULL; |
50 | 50 |
51 // static | 51 // static |
52 void TPMTokenLoader::Initialize() { | 52 void TPMTokenLoader::Initialize() { |
| 53 if (g_tpm_token_loader && g_tpm_token_loader->initialized_for_test_) |
| 54 return; |
53 CHECK(!g_tpm_token_loader); | 55 CHECK(!g_tpm_token_loader); |
54 g_tpm_token_loader = new TPMTokenLoader(false /*for_test*/); | 56 g_tpm_token_loader = new TPMTokenLoader(false /*for_test*/); |
55 } | 57 } |
56 | 58 |
57 // static | 59 // static |
58 void TPMTokenLoader::InitializeForTest() { | 60 void TPMTokenLoader::InitializeForTest() { |
59 CHECK(!g_tpm_token_loader); | 61 CHECK(!g_tpm_token_loader); |
60 g_tpm_token_loader = new TPMTokenLoader(true /*for_test*/); | 62 g_tpm_token_loader = new TPMTokenLoader(true /*for_test*/); |
61 } | 63 } |
62 | 64 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner) { | 101 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner) { |
100 crypto_task_runner_ = crypto_task_runner; | 102 crypto_task_runner_ = crypto_task_runner; |
101 MaybeStartTokenInitialization(); | 103 MaybeStartTokenInitialization(); |
102 } | 104 } |
103 | 105 |
104 TPMTokenLoader::~TPMTokenLoader() { | 106 TPMTokenLoader::~TPMTokenLoader() { |
105 if (!initialized_for_test_ && LoginState::IsInitialized()) | 107 if (!initialized_for_test_ && LoginState::IsInitialized()) |
106 LoginState::Get()->RemoveObserver(this); | 108 LoginState::Get()->RemoveObserver(this); |
107 } | 109 } |
108 | 110 |
109 void TPMTokenLoader::AddObserver(TPMTokenLoader::Observer* observer) { | 111 TPMTokenLoader::TPMTokenStatus TPMTokenLoader::IsTPMTokenEnabled( |
110 observers_.AddObserver(observer); | 112 const TPMReadyCallback& callback) { |
| 113 if (tpm_token_state_ == TPM_TOKEN_INITIALIZED) |
| 114 return TPM_TOKEN_STATUS_ENABLED; |
| 115 if (!IsTPMLoadingEnabled() || tpm_token_state_ == TPM_DISABLED) |
| 116 return TPM_TOKEN_STATUS_DISABLED; |
| 117 // Status is not known yet. |
| 118 if (!callback.is_null()) |
| 119 tpm_ready_callback_list_.push_back(callback); |
| 120 return TPM_TOKEN_STATUS_UNDETERMINED; |
111 } | 121 } |
112 | 122 |
113 void TPMTokenLoader::RemoveObserver(TPMTokenLoader::Observer* observer) { | 123 bool TPMTokenLoader::IsTPMLoadingEnabled() const { |
114 observers_.RemoveObserver(observer); | 124 // TPM loading is enabled on non-ChromeOS environments, e.g. when running |
115 } | 125 // tests on Linux. |
116 | 126 // Treat TPM as disabled for guest users since they do not store certs. |
117 bool TPMTokenLoader::IsTPMTokenReady() const { | 127 return initialized_for_test_ || (base::SysInfo::IsRunningOnChromeOS() && |
118 return tpm_token_state_ == TPM_DISABLED || | 128 LoginState::Get()->IsGuestSessionUser()); |
119 tpm_token_state_ == TPM_TOKEN_INITIALIZED; | |
120 } | 129 } |
121 | 130 |
122 void TPMTokenLoader::MaybeStartTokenInitialization() { | 131 void TPMTokenLoader::MaybeStartTokenInitialization() { |
123 CHECK(thread_checker_.CalledOnValidThread()); | 132 CHECK(thread_checker_.CalledOnValidThread()); |
124 | 133 |
125 // This is the entry point to the TPM token initialization process, | 134 // This is the entry point to the TPM token initialization process, |
126 // which we should do at most once. | 135 // which we should do at most once. |
127 if (tpm_token_state_ != TPM_STATE_UNKNOWN || !crypto_task_runner_.get()) | 136 if (tpm_token_state_ != TPM_STATE_UNKNOWN || !crypto_task_runner_.get()) |
128 return; | 137 return; |
129 | 138 |
130 if (!LoginState::IsInitialized()) | 139 if (!LoginState::IsInitialized()) |
131 return; | 140 return; |
132 | 141 |
133 bool start_initialization = LoginState::Get()->IsUserLoggedIn(); | 142 bool start_initialization = LoginState::Get()->IsUserLoggedIn(); |
134 | 143 |
135 VLOG(1) << "StartTokenInitialization: " << start_initialization; | 144 VLOG(1) << "StartTokenInitialization: " << start_initialization; |
136 if (!start_initialization) | 145 if (!start_initialization) |
137 return; | 146 return; |
138 | 147 |
139 if (!base::SysInfo::IsRunningOnChromeOS()) | 148 if (!IsTPMLoadingEnabled()) |
140 tpm_token_state_ = TPM_DISABLED; | |
141 | |
142 // Treat TPM as disabled for guest users since they do not store certs. | |
143 if (LoginState::Get()->IsGuestSessionUser()) | |
144 tpm_token_state_ = TPM_DISABLED; | 149 tpm_token_state_ = TPM_DISABLED; |
145 | 150 |
146 ContinueTokenInitialization(); | 151 ContinueTokenInitialization(); |
147 | 152 |
148 DCHECK_NE(tpm_token_state_, TPM_STATE_UNKNOWN); | 153 DCHECK_NE(tpm_token_state_, TPM_STATE_UNKNOWN); |
149 } | 154 } |
150 | 155 |
151 void TPMTokenLoader::ContinueTokenInitialization() { | 156 void TPMTokenLoader::ContinueTokenInitialization() { |
152 CHECK(thread_checker_.CalledOnValidThread()); | 157 CHECK(thread_checker_.CalledOnValidThread()); |
153 VLOG(1) << "ContinueTokenInitialization: " << tpm_token_state_; | 158 VLOG(1) << "ContinueTokenInitialization: " << tpm_token_state_; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 | 238 |
234 if (call_status == DBUS_METHOD_CALL_SUCCESS && tpm_is_enabled) | 239 if (call_status == DBUS_METHOD_CALL_SUCCESS && tpm_is_enabled) |
235 tpm_token_state_ = TPM_ENABLED; | 240 tpm_token_state_ = TPM_ENABLED; |
236 else | 241 else |
237 tpm_token_state_ = TPM_DISABLED; | 242 tpm_token_state_ = TPM_DISABLED; |
238 | 243 |
239 ContinueTokenInitialization(); | 244 ContinueTokenInitialization(); |
240 } | 245 } |
241 | 246 |
242 void TPMTokenLoader::OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, | 247 void TPMTokenLoader::OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, |
243 bool is_tpm_token_ready) { | 248 bool is_tpm_token_ready) { |
244 VLOG(1) << "OnPkcs11IsTpmTokenReady: " << is_tpm_token_ready; | 249 VLOG(1) << "OnPkcs11IsTpmTokenReady: " << is_tpm_token_ready; |
245 | 250 |
246 if (call_status == DBUS_METHOD_CALL_FAILURE || !is_tpm_token_ready) { | 251 if (call_status == DBUS_METHOD_CALL_FAILURE || !is_tpm_token_ready) { |
247 RetryTokenInitializationLater(); | 252 RetryTokenInitializationLater(); |
248 return; | 253 return; |
249 } | 254 } |
250 | 255 |
251 tpm_token_state_ = TPM_TOKEN_READY; | 256 tpm_token_state_ = TPM_TOKEN_READY; |
252 ContinueTokenInitialization(); | 257 ContinueTokenInitialization(); |
253 } | 258 } |
(...skipping 20 matching lines...) Expand all Loading... |
274 VLOG(1) << "OnTPMTokenInitialized: " << success; | 279 VLOG(1) << "OnTPMTokenInitialized: " << success; |
275 if (!success) { | 280 if (!success) { |
276 RetryTokenInitializationLater(); | 281 RetryTokenInitializationLater(); |
277 return; | 282 return; |
278 } | 283 } |
279 tpm_token_state_ = TPM_TOKEN_INITIALIZED; | 284 tpm_token_state_ = TPM_TOKEN_INITIALIZED; |
280 ContinueTokenInitialization(); | 285 ContinueTokenInitialization(); |
281 } | 286 } |
282 | 287 |
283 void TPMTokenLoader::NotifyTPMTokenReady() { | 288 void TPMTokenLoader::NotifyTPMTokenReady() { |
284 FOR_EACH_OBSERVER(Observer, observers_, OnTPMTokenReady()); | 289 DCHECK(tpm_token_state_ == TPM_DISABLED || |
| 290 tpm_token_state_ == TPM_TOKEN_INITIALIZED); |
| 291 bool tpm_status = tpm_token_state_ == TPM_TOKEN_INITIALIZED; |
| 292 for (TPMReadyCallbackList::iterator i = tpm_ready_callback_list_.begin(); |
| 293 i != tpm_ready_callback_list_.end(); |
| 294 ++i) { |
| 295 i->Run(tpm_status); |
| 296 } |
| 297 tpm_ready_callback_list_.clear(); |
285 } | 298 } |
286 | 299 |
287 void TPMTokenLoader::LoggedInStateChanged() { | 300 void TPMTokenLoader::LoggedInStateChanged() { |
288 VLOG(1) << "LoggedInStateChanged"; | 301 VLOG(1) << "LoggedInStateChanged"; |
289 MaybeStartTokenInitialization(); | 302 MaybeStartTokenInitialization(); |
290 } | 303 } |
291 | 304 |
292 } // namespace chromeos | 305 } // namespace chromeos |
OLD | NEW |