| OLD | NEW |
| (Empty) |
| 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 #include <vector> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/observer_list.h" | |
| 15 #include "base/threading/thread_checker.h" | |
| 16 #include "chromeos/chromeos_export.h" | |
| 17 #include "chromeos/login/login_state.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class SequencedTaskRunner; | |
| 21 } | |
| 22 | |
| 23 namespace chromeos { | |
| 24 | |
| 25 struct TPMTokenInfo; | |
| 26 class TPMTokenInfoGetter; | |
| 27 | |
| 28 // This class is responsible for loading the TPM backed token for the system | |
| 29 // slot when the user logs in. It is expected to be constructed on the UI thread | |
| 30 // and public methods should all be called from the UI thread. | |
| 31 // When the TPM token is loaded, or if the TPM should stay disabled for the | |
| 32 // session, the observers are notified using |OnTPMTokenReady|. | |
| 33 // Note: This currently initializes the token with the hard coded default id 0. | |
| 34 // See CryptohomeClient::OnPkcs11GetTpmTokenInfo. | |
| 35 class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer { | |
| 36 public: | |
| 37 enum TPMTokenStatus { | |
| 38 TPM_TOKEN_STATUS_UNDETERMINED, | |
| 39 TPM_TOKEN_STATUS_ENABLED, | |
| 40 TPM_TOKEN_STATUS_DISABLED | |
| 41 }; | |
| 42 | |
| 43 typedef base::Callback<void(bool)> TPMReadyCallback; | |
| 44 typedef std::vector<TPMReadyCallback> TPMReadyCallbackList; | |
| 45 | |
| 46 // Sets the global instance. Must be called before any calls to Get(). | |
| 47 // The global instance will immediately start observing |LoginState|. | |
| 48 static void Initialize(); | |
| 49 | |
| 50 // Sets the global. stubbed out, instance. To be used in tests. | |
| 51 static void InitializeForTest(); | |
| 52 | |
| 53 // Destroys the global instance. | |
| 54 static void Shutdown(); | |
| 55 | |
| 56 // Gets the global instance. Initialize() must be called before this. | |
| 57 static TPMTokenLoader* Get(); | |
| 58 | |
| 59 // Returns true if the global instance has been initialized. | |
| 60 static bool IsInitialized(); | |
| 61 | |
| 62 // |crypto_task_runner| is the task runner that any synchronous crypto calls | |
| 63 // should be made from, e.g. in Chrome this is the IO thread. Must be called | |
| 64 // after the thread is started. When called, this will attempt to start TPM | |
| 65 // token loading. | |
| 66 void SetCryptoTaskRunner( | |
| 67 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner); | |
| 68 | |
| 69 // Checks if the TPM token is enabled. If the state is unknown, |callback| | |
| 70 // will be called back once the TPM state is known. | |
| 71 TPMTokenStatus IsTPMTokenEnabled(const TPMReadyCallback& callback); | |
| 72 | |
| 73 std::string tpm_user_pin() const { return tpm_user_pin_; } | |
| 74 | |
| 75 private: | |
| 76 explicit TPMTokenLoader(bool for_test); | |
| 77 ~TPMTokenLoader() override; | |
| 78 | |
| 79 bool IsTPMLoadingEnabled() const; | |
| 80 | |
| 81 // Starts tpm token initialization if the user is logged in and the crypto | |
| 82 // task runner is set. | |
| 83 void MaybeStartTokenInitialization(); | |
| 84 | |
| 85 // This is the cyclic chain of callbacks to initialize the TPM token. | |
| 86 void ContinueTokenInitialization(); | |
| 87 void OnTPMTokenEnabledForNSS(); | |
| 88 void OnGotTpmTokenInfo(const TPMTokenInfo& token_info); | |
| 89 void OnTPMTokenInitialized(bool success); | |
| 90 | |
| 91 // Notifies observers that the TPM token is ready. | |
| 92 void NotifyTPMTokenReady(); | |
| 93 | |
| 94 // LoginState::Observer | |
| 95 void LoggedInStateChanged() override; | |
| 96 | |
| 97 bool initialized_for_test_; | |
| 98 | |
| 99 TPMReadyCallbackList tpm_ready_callback_list_; | |
| 100 | |
| 101 // The states are traversed in this order but some might get omitted or never | |
| 102 // be left. | |
| 103 enum TPMTokenState { | |
| 104 TPM_STATE_UNKNOWN, | |
| 105 TPM_INITIALIZATION_STARTED, | |
| 106 TPM_TOKEN_ENABLED_FOR_NSS, | |
| 107 TPM_DISABLED, | |
| 108 TPM_TOKEN_INFO_RECEIVED, | |
| 109 TPM_TOKEN_INITIALIZED, | |
| 110 }; | |
| 111 TPMTokenState tpm_token_state_; | |
| 112 | |
| 113 scoped_ptr<TPMTokenInfoGetter> tpm_token_info_getter_; | |
| 114 | |
| 115 // Cached TPM token info. | |
| 116 int tpm_token_slot_id_; | |
| 117 std::string tpm_user_pin_; | |
| 118 | |
| 119 base::ThreadChecker thread_checker_; | |
| 120 | |
| 121 // TaskRunner for crypto calls. | |
| 122 scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_; | |
| 123 | |
| 124 base::WeakPtrFactory<TPMTokenLoader> weak_factory_; | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader); | |
| 127 }; | |
| 128 | |
| 129 } // namespace chromeos | |
| 130 | |
| 131 #endif // CHROMEOS_TPM_TOKEN_LOADER_H_ | |
| OLD | NEW |