Chromium Code Reviews| Index: chrome/browser/chromeos/login/easy_unlock/easy_unlock_tpm_key_manager.h |
| diff --git a/chrome/browser/chromeos/login/easy_unlock/easy_unlock_tpm_key_manager.h b/chrome/browser/chromeos/login/easy_unlock/easy_unlock_tpm_key_manager.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0d25c3b006d36d2dafb437b321538fa4a3d6595c |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/login/easy_unlock/easy_unlock_tpm_key_manager.h |
| @@ -0,0 +1,133 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_EASY_UNLOCK_EASY_UNLOCK_TPM_KEY_MANAGER_H_ |
| +#define CHROME_BROWSER_CHROMEOS_LOGIN_EASY_UNLOCK_EASY_UNLOCK_TPM_KEY_MANAGER_H_ |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/callback.h" |
| +#include "base/macros.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "components/keyed_service/core/keyed_service.h" |
| +#include "crypto/scoped_nss_types.h" |
| + |
| +class PrefRegistrySimple; |
| +class PrefService; |
| + |
| +// Manages per user RSA keys stored in system TPM slot used in easy signin |
| +// protocol. The keys are used to sign a nonce exchanged during signin. |
| +class EasyUnlockTpmKeyManager : public KeyedService { |
|
pneubeck (no reviews)
2014/12/05 12:16:05
I find the state transitions of this implementatio
tbarzic
2014/12/11 23:43:28
Done.
|
| + public: |
| + // Registers local state prefs used to store public RSA keys per user. |
| + static void RegisterLocalStatePrefs(PrefRegistrySimple* registry); |
| + |
| + // Clears local state for user. Should be called when a user is removed. |
| + static void ResetLocalStateForUser(const std::string& user_id); |
| + |
| + // |user_id|: Id for the user associated with the service. Empty for signin |
| + // service. |
| + // |local_state|: The local state prefs. |
| + EasyUnlockTpmKeyManager(const std::string& user_id, PrefService* local_state); |
| + ~EasyUnlockTpmKeyManager() override; |
| + |
| + // Checks if the RSA public key is set in the local state. If not, creates |
| + // one. If the key presence can be confirmed, immediately returns true and |
| + // |callback| never gets called, otherwise returns false (callback is called |
| + // when the key presence is confirmed). |
| + // Must not be called for signin profile. |
| + // |check_private_key|: If public RSA key is set in the local state, whether |
| + // the method should confirm that the private key is present in the system |
| + // slot. If the private key cannot be found, a new key pair will be |
| + // created for the user. |
| + // Note: Checking TPM for the private key is more expensive than only |
| + // checking local state, so setting this to |false| should be preferable. |
| + // Generally, if public key is set in local state, the private key should |
| + // be present in the system TPM slot. This is used to make sure that easy |
| + // signin does not remain permanently broken if something goes wrong. |
| + // |callback|: If the method cannot return immediately, called when the key |
| + // pair presence is confirmed (or a key pair for the user is created). |
| + bool PrepareTpmKey(const std::string& user_id, |
| + bool check_private_key, |
| + const base::Closure& callback); |
| + |
| + // If called, posts a delayed task that cancels |PrepareTpmKey| in case |
|
pneubeck (no reviews)
2014/12/05 12:16:05
... and cancels all previously started timeouts.
tbarzic
2014/12/11 23:43:28
Done.
|
| + // getting system slot takes more than |timeout_ms|. In the case getting |
| + // system slot times out |PrepareTpmKey| callback will be called with an empty |
|
pneubeck (no reviews)
2014/12/05 12:16:05
nit: comma after 'out'
tbarzic
2014/12/11 23:43:28
Done.
|
| + // public key. |
| + bool SetGetSystemSlotTimeoutMs(size_t timeout_ms); |
|
pneubeck (no reviews)
2014/12/05 12:16:05
currently must be called after PrepareTpmKey to ha
tbarzic
2014/12/11 23:43:28
Done.
|
| + |
| + // Gets the public RSA key for user. The key is retrieved from local state. |
| + std::string GetPublicTpmKey(const std::string& user_id); |
| + |
| + // Signs |data| using private RSA key associated with |user_id| stored in TPM |
| + // system slot. |
| + void SignUsingTpmKey( |
| + const std::string& user_id, |
| + const std::string& data, |
| + const base::Callback<void(const std::string& data)> callback); |
| + |
| + private: |
| + // Utility method for setting public key values in local state. |
| + // Note that the keys are saved base64 encoded. |
| + void SetKeyInLocalState(const std::string& user_id, |
| + const std::string& value); |
| + |
| + // Called when TPM system slot is initialized and ready to be used. |
| + // It creates RSA key pair for the user in the system slot. |
| + // When the key pair is created, |OnTpmKeyCreated| will be called with the |
| + // created public key. |
| + // The key will not be created if |public_key| is non-empty and the associated |
| + // private key can be found in the slot. Instead |OnTpmKeyCreated| will be |
| + // called with |public_key|. |
| + void CreateKeyInSystemSlot( |
| + const std::string& user_id, |
| + const std::string& public_key, |
| + crypto::ScopedPK11Slot system_slot); |
| + |
| + // Called when TPM system slot is initialized and ready to be used. |
| + // It schedules data signing operation on a worker thread. The data is signed |
| + // by a private key stored in |system_slot| and identified by |public_key| |
| + // (a private key that is part of the same RSA key pair as |public_key|). |
| + // Once data is signed |callback| is called with the signed data. |
| + void SignDataWithSystemSlot( |
| + const std::string& public_key, |
| + const std::string& data, |
| + const base::Callback<void(const std::string& data)> callback, |
| + crypto::ScopedPK11Slot system_slot); |
| + |
| + // Called when a RSA key pair is created for a user in TPM system slot. |
| + // It saves the pulic key in the local state and runs queued up |
| + // |PrepareTpmKey| callbacks. |
| + void OnTpmKeyCreated(const std::string& user_id, |
| + const std::string& public_key); |
| + |
| + // Called when data signing requested in |SignUsingTpmKey| is done. |
| + // It runs |callback| with the created |signature|. On error the callback will |
| + // be run with an empty string. |
| + void OnDataSigned( |
| + const base::Callback<void(const std::string&)>& callback, |
| + const std::string& signature); |
| + |
| + std::string user_id_; |
| + |
| + PrefService* local_state_; |
| + |
| + // Whether TPM key pair is being created as a result of call to |
| + // |PrepareTpmKey|. If set, callbacks for further |PrepareTpmKey| |
| + // will be queued up and run when the original function call finishes. |
| + bool creating_tpm_key_pair_; |
| + bool got_tpm_slot_; |
| + |
| + // Queued up |PrepareTpmKey| callbacks. |
| + std::vector<base::Closure> tpm_key_present_callbacks_; |
| + |
| + base::WeakPtrFactory<EasyUnlockTpmKeyManager> get_tpm_slot_weak_ptr_factory_; |
| + base::WeakPtrFactory<EasyUnlockTpmKeyManager> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(EasyUnlockTpmKeyManager); |
| +}; |
| + |
| +#endif // CHROME_BROWSER_CHROMEOS_LOGIN_EASY_UNLOCK_EASY_UNLOCK_TPM_KEY_MANAGER_H_ |