| 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 15 matching lines...) Expand all Loading... |
| 26 // This class is responsible for loading the TPM token when the user logs | 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 | 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, | 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 | 29 // or if the TPM should stay disabled for the session, the observers are |
| 30 // notified using |OnTPMTokenReady|. | 30 // notified using |OnTPMTokenReady|. |
| 31 class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer { | 31 class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer { |
| 32 public: | 32 public: |
| 33 class Observer { | 33 class Observer { |
| 34 public: | 34 public: |
| 35 // Called when the TPM token initialization is done or the case where TPM | 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 | 36 // should stay disabled is detected (e.g. on guest login). |
| 37 // disabled, |tpm_user_pin|, |tpm_token_name| and |tpm_token_slot_id| will | 37 virtual void OnTPMTokenReady() = 0; |
| 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; | |
| 42 | 38 |
| 43 protected: | 39 protected: |
| 44 virtual ~Observer() {} | 40 virtual ~Observer() {} |
| 45 }; | 41 }; |
| 46 | 42 |
| 47 // Sets the global instance. Must be called before any calls to Get(). | 43 // Sets the global instance. Must be called before any calls to Get(). |
| 48 // The global instance will immediately start observing |LoginState|. | 44 // The global instance will immediately start observing |LoginState|. |
| 49 static void Initialize(); | 45 static void Initialize(); |
| 50 | 46 |
| 47 // Sets the global. stubbed out, instance. To be used in tests. |
| 48 static void InitializeForTest(); |
| 49 |
| 51 // Destroys the global instance. | 50 // Destroys the global instance. |
| 52 static void Shutdown(); | 51 static void Shutdown(); |
| 53 | 52 |
| 54 // Gets the global instance. Initialize() must be called before this. | 53 // Gets the global instance. Initialize() must be called before this. |
| 55 static TPMTokenLoader* Get(); | 54 static TPMTokenLoader* Get(); |
| 56 | 55 |
| 57 // Returns true if the global instance has been initialized. | 56 // Returns true if the global instance has been initialized. |
| 58 static bool IsInitialized(); | 57 static bool IsInitialized(); |
| 59 | 58 |
| 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(); | |
| 64 | |
| 65 // |crypto_task_runner| is the task runner that any synchronous crypto calls | 59 // |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 | 60 // 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 | 61 // after the thread is started. When called, this will attempt to start TPM |
| 68 // token loading. | 62 // token loading. |
| 69 void SetCryptoTaskRunner( | 63 void SetCryptoTaskRunner( |
| 70 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner); | 64 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner); |
| 71 | 65 |
| 72 void AddObserver(TPMTokenLoader::Observer* observer); | 66 void AddObserver(TPMTokenLoader::Observer* observer); |
| 73 void RemoveObserver(TPMTokenLoader::Observer* observer); | 67 void RemoveObserver(TPMTokenLoader::Observer* observer); |
| 74 | 68 |
| 75 // Checks if the TPM token in ready to be used. | 69 // Checks if the TPM token in ready to be used. |
| 76 bool IsTPMTokenReady() const; | 70 bool IsTPMTokenReady() const; |
| 77 | 71 |
| 72 std::string tpm_user_pin() const { return tpm_user_pin_; } |
| 73 |
| 78 private: | 74 private: |
| 79 TPMTokenLoader(); | 75 explicit TPMTokenLoader(bool for_test); |
| 80 virtual ~TPMTokenLoader(); | 76 virtual ~TPMTokenLoader(); |
| 81 | 77 |
| 82 // Starts tpm token initialization if the user is logged in and the crypto | 78 // Starts tpm token initialization if the user is logged in and the crypto |
| 83 // task runner is set. | 79 // task runner is set. |
| 84 void MaybeStartTokenInitialization(); | 80 void MaybeStartTokenInitialization(); |
| 85 | 81 |
| 86 // This is the cyclic chain of callbacks to initialize the TPM token. | 82 // This is the cyclic chain of callbacks to initialize the TPM token. |
| 87 void ContinueTokenInitialization(); | 83 void ContinueTokenInitialization(); |
| 88 void OnPersistentNSSDBOpened(); | 84 void OnPersistentNSSDBOpened(); |
| 89 void OnTpmIsEnabled(DBusMethodCallStatus call_status, | 85 void OnTpmIsEnabled(DBusMethodCallStatus call_status, |
| 90 bool tpm_is_enabled); | 86 bool tpm_is_enabled); |
| 91 void OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, | 87 void OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, |
| 92 bool is_tpm_token_ready); | 88 bool is_tpm_token_ready); |
| 93 void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status, | 89 void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status, |
| 94 const std::string& token_name, | 90 const std::string& token_name, |
| 95 const std::string& user_pin, | 91 const std::string& user_pin, |
| 96 int token_slot_id); | 92 int token_slot_id); |
| 97 void OnTPMTokenInitialized(bool success); | 93 void OnTPMTokenInitialized(bool success); |
| 98 | 94 |
| 99 // If token initialization step fails (e.g. if tpm token is not yet ready) | 95 // If token initialization step fails (e.g. if tpm token is not yet ready) |
| 100 // schedules the initialization step retry attempt after a timeout. | 96 // schedules the initialization step retry attempt after a timeout. |
| 101 void RetryTokenInitializationLater(); | 97 void RetryTokenInitializationLater(); |
| 102 | 98 |
| 103 // Notifies observers that the TPM token is ready. | 99 // Notifies observers that the TPM token is ready. |
| 104 void NotifyTPMTokenReady(); | 100 void NotifyTPMTokenReady(); |
| 105 | 101 |
| 106 // LoginState::Observer | 102 // LoginState::Observer |
| 107 virtual void LoggedInStateChanged() OVERRIDE; | 103 virtual void LoggedInStateChanged() OVERRIDE; |
| 108 | 104 |
| 109 bool initialize_tpm_for_test_; | 105 bool initialized_for_test_; |
| 110 | 106 |
| 111 ObserverList<Observer> observers_; | 107 ObserverList<Observer> observers_; |
| 112 | 108 |
| 113 // The states are traversed in this order but some might get omitted or never | 109 // The states are traversed in this order but some might get omitted or never |
| 114 // be left. | 110 // be left. |
| 115 enum TPMTokenState { | 111 enum TPMTokenState { |
| 116 TPM_STATE_UNKNOWN, | 112 TPM_STATE_UNKNOWN, |
| 117 TPM_INITIALIZATION_STARTED, | 113 TPM_INITIALIZATION_STARTED, |
| 118 TPM_DB_OPENED, | 114 TPM_DB_OPENED, |
| 119 TPM_DISABLED, | 115 TPM_DISABLED, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 139 scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_; | 135 scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_; |
| 140 | 136 |
| 141 base::WeakPtrFactory<TPMTokenLoader> weak_factory_; | 137 base::WeakPtrFactory<TPMTokenLoader> weak_factory_; |
| 142 | 138 |
| 143 DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader); | 139 DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader); |
| 144 }; | 140 }; |
| 145 | 141 |
| 146 } // namespace chromeos | 142 } // namespace chromeos |
| 147 | 143 |
| 148 #endif // CHROMEOS_TPM_TOKEN_LOADER_H_ | 144 #endif // CHROMEOS_TPM_TOKEN_LOADER_H_ |
| OLD | NEW |