forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpm_password_fetcher.cc
73 lines (59 loc) · 2.23 KB
/
tpm_password_fetcher.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 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.
#include "chromeos/tpm/tpm_password_fetcher.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chromeos/dbus/cryptohome_client.h"
#include "chromeos/dbus/dbus_thread_manager.h"
namespace chromeos {
namespace {
// Interval between TPM password checks.
const int kTpmCheckIntervalMs = 500;
} // namespace
TpmPasswordFetcher::TpmPasswordFetcher(TpmPasswordFetcherDelegate* delegate)
: delegate_(delegate), weak_factory_(this) {
DCHECK(delegate_);
}
TpmPasswordFetcher::~TpmPasswordFetcher() = default;
void TpmPasswordFetcher::Fetch() {
// Since this method is also called directly.
weak_factory_.InvalidateWeakPtrs();
DBusThreadManager::Get()->GetCryptohomeClient()->TpmIsReady(base::BindOnce(
&TpmPasswordFetcher::OnTpmIsReady, weak_factory_.GetWeakPtr()));
}
void TpmPasswordFetcher::OnTpmIsReady(base::Optional<bool> tpm_is_ready) {
if (tpm_is_ready.value_or(false)) {
DBusThreadManager::Get()->GetCryptohomeClient()->TpmGetPassword(
base::BindOnce(&TpmPasswordFetcher::OnTpmGetPassword,
weak_factory_.GetWeakPtr()));
} else {
// Password hasn't been acquired, reschedule fetch.
RescheduleFetch();
}
}
void TpmPasswordFetcher::OnTpmGetPassword(
base::Optional<std::string> password) {
if (password) {
if (password->empty()) {
// For a fresh OOBE flow TPM is uninitialized,
// ownership process is started at the EULA screen,
// password is cleared after EULA is accepted.
LOG(ERROR) << "TPM returned an empty password.";
}
delegate_->OnPasswordFetched(*password);
} else {
// Password hasn't been acquired, reschedule fetch.
RescheduleFetch();
}
}
void TpmPasswordFetcher::RescheduleFetch() {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&TpmPasswordFetcher::Fetch, weak_factory_.GetWeakPtr()),
base::TimeDelta::FromMilliseconds(kTpmCheckIntervalMs));
}
} // namespace chromeos