tpm_token_info_getter.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef ASH_COMPONENTS_TPM_TPM_TOKEN_INFO_GETTER_H_
  5. #define ASH_COMPONENTS_TPM_TPM_TOKEN_INFO_GETTER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/component_export.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/time/time.h"
  13. #include "chromeos/ash/components/dbus/cryptohome/UserDataAuth.pb.h"
  14. #include "chromeos/ash/components/dbus/userdataauth/cryptohome_pkcs11_client.h"
  15. #include "chromeos/dbus/tpm_manager/tpm_manager.pb.h"
  16. #include "components/account_id/account_id.h"
  17. #include "third_party/abseil-cpp/absl/types/optional.h"
  18. namespace base {
  19. class TaskRunner;
  20. }
  21. namespace ash {
  22. // Class for getting a user or the system TPM token info from cryptohome during
  23. // TPM token loading.
  24. class COMPONENT_EXPORT(ASH_COMPONENTS_TPM) TPMTokenInfoGetter {
  25. public:
  26. using TpmTokenInfoCallback = base::OnceCallback<void(
  27. absl::optional<user_data_auth::TpmTokenInfo> token_info)>;
  28. // Factory method for TPMTokenInfoGetter for a user token.
  29. static std::unique_ptr<TPMTokenInfoGetter> CreateForUserToken(
  30. const AccountId& account_id,
  31. CryptohomePkcs11Client* cryptohome_pkcs11_client,
  32. const scoped_refptr<base::TaskRunner>& delayed_task_runner);
  33. // Factory method for TPMTokenGetter for the system token.
  34. static std::unique_ptr<TPMTokenInfoGetter> CreateForSystemToken(
  35. CryptohomePkcs11Client* cryptohome_pkcs11_client,
  36. const scoped_refptr<base::TaskRunner>& delayed_task_runner);
  37. TPMTokenInfoGetter(const TPMTokenInfoGetter&) = delete;
  38. TPMTokenInfoGetter& operator=(const TPMTokenInfoGetter&) = delete;
  39. ~TPMTokenInfoGetter();
  40. // Starts getting TPM token info. Should be called at most once.
  41. // |callback| will be called when all the info is fetched.
  42. // The object may get deleted before |callback| is called, which is equivalent
  43. // to cancelling the info getting (in which case |callback| will never get
  44. // called).
  45. void Start(TpmTokenInfoCallback callback);
  46. void set_nss_slots_software_fallback_for_testing(
  47. bool use_nss_slots_software_fallback) {
  48. use_nss_slots_software_fallback_ = use_nss_slots_software_fallback;
  49. }
  50. private:
  51. enum Type {
  52. TYPE_SYSTEM,
  53. TYPE_USER
  54. };
  55. enum State {
  56. STATE_INITIAL,
  57. STATE_STARTED,
  58. STATE_TPM_ENABLED,
  59. STATE_NSS_SLOTS_SOFTWARE_FALLBACK,
  60. STATE_DONE
  61. };
  62. TPMTokenInfoGetter(
  63. Type type,
  64. const AccountId& account_id,
  65. CryptohomePkcs11Client* cryptohome_pkcs11_client,
  66. const scoped_refptr<base::TaskRunner>& delayed_task_runner);
  67. // Continues TPM token info getting procedure by starting the task associated
  68. // with the current TPMTokenInfoGetter state.
  69. void Continue();
  70. // If token initialization step fails (e.g. if tpm token is not yet ready)
  71. // schedules the initialization step retry attempt after a timeout.
  72. void RetryLater();
  73. // Callbacks for TpmManagerClient.
  74. void OnGetTpmStatus(
  75. const ::tpm_manager::GetTpmNonsensitiveStatusReply& reply);
  76. // Cryptohome methods callbacks.
  77. void OnPkcs11GetTpmTokenInfo(
  78. absl::optional<user_data_auth::Pkcs11GetTpmTokenInfoReply> token_info);
  79. // The task runner used to run delayed tasks when retrying failed Cryptohome
  80. // calls.
  81. scoped_refptr<base::TaskRunner> delayed_task_runner_;
  82. Type type_;
  83. State state_;
  84. // The account id associated with the TPMTokenInfoGetter. Empty for system
  85. // token.
  86. AccountId account_id_;
  87. TpmTokenInfoCallback callback_;
  88. // If set and the TPM is not owned, TPMTokenInfoGetter will still get the
  89. // token info using cryptohome's Pkcs11GetTpmTokenInfo query. The token info
  90. // is needed for falling back to a software-backed initialization of the
  91. // system token.
  92. bool use_nss_slots_software_fallback_ = false;
  93. // The current request delay before the next attempt to initialize the
  94. // TPM. Will be adapted after each attempt.
  95. base::TimeDelta tpm_request_delay_;
  96. CryptohomePkcs11Client* cryptohome_pkcs11_client_;
  97. base::WeakPtrFactory<TPMTokenInfoGetter> weak_factory_{this};
  98. };
  99. } // namespace ash
  100. #endif // ASH_COMPONENTS_TPM_TPM_TOKEN_INFO_GETTER_H_