Commit b8da7911 authored by Leo Lai's avatar Leo Lai Committed by Chromium LUCI CQ

SystemTokenCertDBInitializer always use TpmManagerClient to check TPM.

we are migrating tpm status calls to TpmManagerClient.

BUG=b:172748724
TEST=unit_tests.

Change-Id: Ib5cabe3deacf4ea26f1663086b6cf9a9fcf001f0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2581705Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Reviewed-by: default avatarAchuith Bhandarkar <achuith@chromium.org>
Commit-Queue: Leo Lai <cylai@google.com>
Cr-Commit-Position: refs/heads/master@{#836482}
parent 8d82de3d
...@@ -61,8 +61,8 @@ ...@@ -61,8 +61,8 @@
#include "chrome/test/base/ui_test_utils.h" #include "chrome/test/base/ui_test_utils.h"
#include "chromeos/constants/chromeos_features.h" #include "chromeos/constants/chromeos_features.h"
#include "chromeos/constants/chromeos_switches.h" #include "chromeos/constants/chromeos_switches.h"
#include "chromeos/dbus/cryptohome/fake_cryptohome_client.h"
#include "chromeos/dbus/session_manager/fake_session_manager_client.h" #include "chromeos/dbus/session_manager/fake_session_manager_client.h"
#include "chromeos/dbus/tpm_manager/fake_tpm_manager_client.h"
#include "chromeos/dbus/tpm_manager/tpm_manager_client.h" #include "chromeos/dbus/tpm_manager/tpm_manager_client.h"
#include "chromeos/tpm/tpm_token_loader.h" #include "chromeos/tpm/tpm_token_loader.h"
#include "components/content_settings/core/common/pref_names.h" #include "components/content_settings/core/common/pref_names.h"
...@@ -1161,9 +1161,15 @@ IN_PROC_BROWSER_TEST_F(WebviewClientCertsLoginTest, ...@@ -1161,9 +1161,15 @@ IN_PROC_BROWSER_TEST_F(WebviewClientCertsLoginTest,
class WebviewClientCertsTokenLoadingLoginTest class WebviewClientCertsTokenLoadingLoginTest
: public WebviewClientCertsLoginTestBase { : public WebviewClientCertsLoginTestBase {
public: public:
WebviewClientCertsTokenLoadingLoginTest() WebviewClientCertsTokenLoadingLoginTest() {
: cryptohome_client_(new FakeCryptohomeClient) { // At very early stage, the system slot is being initialized becuase fake
cryptohome_client_->set_tpm_is_ready(false); // tpm manager tells the TPM is owned by default. So, it has to be overriden
// here instead of in the test body or `SetUpOnMainThread()`.
TpmManagerClient::InitializeFake();
TpmManagerClient::Get()
->GetTestInterface()
->mutable_nonsensitive_status_reply()
->set_is_owned(false);
} }
WebviewClientCertsTokenLoadingLoginTest( WebviewClientCertsTokenLoadingLoginTest(
...@@ -1171,8 +1177,6 @@ class WebviewClientCertsTokenLoadingLoginTest ...@@ -1171,8 +1177,6 @@ class WebviewClientCertsTokenLoadingLoginTest
WebviewClientCertsTokenLoadingLoginTest& operator=( WebviewClientCertsTokenLoadingLoginTest& operator=(
const WebviewClientCertsTokenLoadingLoginTest&) = delete; const WebviewClientCertsTokenLoadingLoginTest&) = delete;
FakeCryptohomeClient* cryptohome_client() { return cryptohome_client_; }
// Prepares a testing system slot (without injecting it as an already // Prepares a testing system slot (without injecting it as an already
// initialized yet) and imports a client certificate into it. // initialized yet) and imports a client certificate into it.
void PrepareSystemSlot() { void PrepareSystemSlot() {
...@@ -1229,9 +1233,6 @@ class WebviewClientCertsTokenLoadingLoginTest ...@@ -1229,9 +1233,6 @@ class WebviewClientCertsTokenLoadingLoginTest
test_system_slot_nss_db_.reset(); test_system_slot_nss_db_.reset();
} }
// Owned by the CryptohomeClient singleton.
FakeCryptohomeClient* cryptohome_client_;
std::unique_ptr<crypto::ScopedTestNSSDB> test_system_slot_nss_db_; std::unique_ptr<crypto::ScopedTestNSSDB> test_system_slot_nss_db_;
}; };
...@@ -1278,7 +1279,10 @@ IN_PROC_BROWSER_TEST_F(WebviewClientCertsTokenLoadingLoginTest, ...@@ -1278,7 +1279,10 @@ IN_PROC_BROWSER_TEST_F(WebviewClientCertsTokenLoadingLoginTest,
// Report the TPM as ready, triggering the system token initialization by // Report the TPM as ready, triggering the system token initialization by
// SystemTokenCertDBInitializer. // SystemTokenCertDBInitializer.
cryptohome_client()->set_tpm_is_ready(true); TpmManagerClient::Get()
->GetTestInterface()
->mutable_nonsensitive_status_reply()
->set_is_owned(true);
TpmManagerClient::Get()->GetTestInterface()->EmitOwnershipTakenSignal(); TpmManagerClient::Get()->GetTestInterface()->EmitOwnershipTakenSignal();
const std::string https_reply_content = const std::string https_reply_content =
......
...@@ -207,16 +207,10 @@ void SystemTokenCertDBInitializer::OnCryptohomeAvailable(bool available) { ...@@ -207,16 +207,10 @@ void SystemTokenCertDBInitializer::OnCryptohomeAvailable(bool available) {
} }
void SystemTokenCertDBInitializer::CheckTpm() { void SystemTokenCertDBInitializer::CheckTpm() {
if (IsSystemSlotSoftwareFallbackEnabled()) { TpmManagerClient::Get()->GetTpmNonsensitiveStatus(
TpmManagerClient::Get()->GetTpmNonsensitiveStatus( ::tpm_manager::GetTpmNonsensitiveStatusRequest(),
::tpm_manager::GetTpmNonsensitiveStatusRequest(), base::BindOnce(&SystemTokenCertDBInitializer::OnGetTpmNonsensitiveStatus,
base::BindOnce(&SystemTokenCertDBInitializer::OnGetTpmStatus, weak_ptr_factory_.GetWeakPtr()));
weak_ptr_factory_.GetWeakPtr()));
} else {
CryptohomeClient::Get()->TpmIsReady(
base::BindOnce(&SystemTokenCertDBInitializer::OnGotTpmIsReady,
weak_ptr_factory_.GetWeakPtr()));
}
} }
void SystemTokenCertDBInitializer::RetryCheckTpmLater() { void SystemTokenCertDBInitializer::RetryCheckTpmLater() {
...@@ -228,7 +222,7 @@ void SystemTokenCertDBInitializer::RetryCheckTpmLater() { ...@@ -228,7 +222,7 @@ void SystemTokenCertDBInitializer::RetryCheckTpmLater() {
tpm_request_delay_ = GetNextRequestDelay(tpm_request_delay_); tpm_request_delay_ = GetNextRequestDelay(tpm_request_delay_);
} }
void SystemTokenCertDBInitializer::OnGetTpmStatus( void SystemTokenCertDBInitializer::OnGetTpmNonsensitiveStatus(
const ::tpm_manager::GetTpmNonsensitiveStatusReply& reply) { const ::tpm_manager::GetTpmNonsensitiveStatusReply& reply) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
...@@ -238,39 +232,35 @@ void SystemTokenCertDBInitializer::OnGetTpmStatus( ...@@ -238,39 +232,35 @@ void SystemTokenCertDBInitializer::OnGetTpmStatus(
return; return;
} }
// When the software fallback flag is set and the TPM is disabled, we skip the // There are 2 cases we start initializing the database at this point: 1. TPM
// TpmIsReady() call. Otherwise, because the TPM won't be ready and will never // is ready, i.e., owned, or 2. TPM is disabled but software fallback is
// be signaled as such, we won't proceed to the database initialization. // allowed. Note that we don't fall back to software solution as long as TPM
if (!reply.is_enabled()) { // is enabled.
if (reply.is_owned() ||
(!reply.is_enabled() && IsSystemSlotSoftwareFallbackEnabled())) {
VLOG_IF(1, !reply.is_owned())
<< "Initializing database when TPM is not owned.";
MaybeStartInitializingDatabase(); MaybeStartInitializingDatabase();
return; return;
} }
CryptohomeClient::Get()->TpmIsReady( // If the TPM is enabled but not owned yet, request taking TPM initialization;
base::BindOnce(&SystemTokenCertDBInitializer::OnGotTpmIsReady, // when it's done, the ownership taken signal triggers database
weak_ptr_factory_.GetWeakPtr())); // initialization.
} if (reply.is_enabled() && !reply.is_owned()) {
void SystemTokenCertDBInitializer::OnGotTpmIsReady(
base::Optional<bool> tpm_is_ready) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!tpm_is_ready.has_value() || !tpm_is_ready.value()) {
VLOG(1) << "SystemTokenCertDBInitializer: TPM is not ready - not loading " VLOG(1) << "SystemTokenCertDBInitializer: TPM is not ready - not loading "
"system token."; "system token.";
if (ShallAttemptTpmOwnership()) { if (ShallAttemptTpmOwnership()) {
// Signal to cryptohome that it can attempt TPM ownership, if it // Requests tpm manager to initialize TPM, if it haven't done that yet.
// haven't done that yet. The previous signal from EULA dialogue could // The previous request from EULA dialogue could have been lost if
// have been lost if initialization was interrupted. // initialization was interrupted. We don't care about the result, and
// We don't care about the result, and don't block waiting for it. // don't block waiting for it.
LOG(WARNING) << "Request attempting TPM ownership."; LOG(WARNING) << "Request taking TPM ownership.";
TpmManagerClient::Get()->TakeOwnership( TpmManagerClient::Get()->TakeOwnership(
::tpm_manager::TakeOwnershipRequest(), base::DoNothing()); ::tpm_manager::TakeOwnershipRequest(), base::DoNothing());
} }
return; return;
} }
MaybeStartInitializingDatabase();
} }
void SystemTokenCertDBInitializer::MaybeStartInitializingDatabase() { void SystemTokenCertDBInitializer::MaybeStartInitializingDatabase() {
......
...@@ -95,19 +95,14 @@ class SystemTokenCertDBInitializer : public TpmManagerClient::Observer { ...@@ -95,19 +95,14 @@ class SystemTokenCertDBInitializer : public TpmManagerClient::Observer {
// schedules the initialization step retry attempt after a timeout. // schedules the initialization step retry attempt after a timeout.
void RetryCheckTpmLater(); void RetryCheckTpmLater();
// This is a callback for the GetTpmNonsensitiveStatus() query. It is only // This is a callback for the GetTpmNonsensitiveStatus() query. 2 main
// called when the build flag system_slot_software_fallback is enabled. If the // operations are performed:
// build flag is enabled and TPM is disabled, we skip the cryptohome // 1. Initializes the database if TPM is owned or software fallback is
// TpmIsReady() check during initialization, otherwise we continue the normal // enabled.
// flow with TpmIsReady() and its callback. // 2. Triggers TPM ownership process if necessary.
void OnGetTpmStatus( void OnGetTpmNonsensitiveStatus(
const ::tpm_manager::GetTpmNonsensitiveStatusReply& reply); const ::tpm_manager::GetTpmNonsensitiveStatusReply& reply);
// This is a callback for the cryptohome TpmIsReady query. Note that this is
// not a listener which would be called once TPM becomes ready if it was not
// ready on startup - that event is observed by `OnOwnershipTakenSignal()`.
void OnGotTpmIsReady(base::Optional<bool> tpm_is_ready);
// Starts loading the system slot and initializing the corresponding NSS cert // Starts loading the system slot and initializing the corresponding NSS cert
// database, unless it was already started before. // database, unless it was already started before.
void MaybeStartInitializingDatabase(); void MaybeStartInitializingDatabase();
......
...@@ -219,7 +219,10 @@ void TpmManagerClient::Initialize(dbus::Bus* bus) { ...@@ -219,7 +219,10 @@ void TpmManagerClient::Initialize(dbus::Bus* bus) {
// static // static
void TpmManagerClient::InitializeFake() { void TpmManagerClient::InitializeFake() {
new FakeTpmManagerClient(); // Do not create a new instance if it was initialized early in a browser test
// (for early setup calls dependent on TpmManagerClient).
if (!FakeTpmManagerClient::Get())
new FakeTpmManagerClient();
} }
// static // static
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment