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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/ash/cryptohome/cryptohome_web_ui_handler.h"
#include <numeric>
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/cryptohome/rpc.pb.h"
#include "chromeos/ash/components/dbus/dbus_thread_manager.h"
#include "chromeos/ash/components/dbus/userdataauth/cryptohome_pkcs11_client.h"
#include "chromeos/ash/components/dbus/userdataauth/userdataauth_client.h"
#include "chromeos/ash/components/login/auth/auth_factor_editor.h"
#include "chromeos/ash/components/login/auth/public/cryptohome_key_constants.h"
#include "chromeos/dbus/tpm_manager/tpm_manager_client.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_ui.h"
#include "crypto/nss_util.h"
using content::BrowserThread;
namespace ash {
namespace {
// Maximal number of RecoveryId entries we want to display.
constexpr int kRecoveryIdHistoryDepth = 5;
void ForwardToUIThread(base::OnceCallback<void(bool)> ui_callback,
bool result) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(std::move(ui_callback), result));
}
user_data_auth::GetAuthFactorExtendedInfoRequest
GenerateAuthFactorExtendedInfoRequest(int depth) {
user_data_auth::GetAuthFactorExtendedInfoRequest req;
user_data_auth::RecoveryExtendedInfoRequest req_extended_info;
const user_manager::User* primary_user =
user_manager::UserManager::Get()->GetPrimaryUser();
if (primary_user) {
*req.mutable_account_id() =
cryptohome::CreateAccountIdentifierFromAccountId(
primary_user->GetAccountId());
*req.mutable_auth_factor_label() = kCryptohomeRecoveryKeyLabel;
req_extended_info.set_max_depth(depth);
*req.mutable_recovery_info_request() = std::move(req_extended_info);
}
return req;
}
} // namespace
CryptohomeWebUIHandler::CryptohomeWebUIHandler() = default;
CryptohomeWebUIHandler::~CryptohomeWebUIHandler() = default;
void CryptohomeWebUIHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"pageLoaded", base::BindRepeating(&CryptohomeWebUIHandler::OnPageLoaded,
weak_ptr_factory_.GetWeakPtr()));
}
void CryptohomeWebUIHandler::OnPageLoaded(const base::Value::List& args) {
UserDataAuthClient* userdataauth_client = UserDataAuthClient::Get();
CryptohomePkcs11Client* cryptohome_pkcs11_client =
CryptohomePkcs11Client::Get();
userdataauth_client->IsMounted(
user_data_auth::IsMountedRequest(),
base::BindOnce(&CryptohomeWebUIHandler::OnIsMounted,
weak_ptr_factory_.GetWeakPtr()));
chromeos::TpmManagerClient::Get()->GetTpmNonsensitiveStatus(
::tpm_manager::GetTpmNonsensitiveStatusRequest(),
base::BindOnce(&CryptohomeWebUIHandler::OnGetTpmStatus,
weak_ptr_factory_.GetWeakPtr()));
cryptohome_pkcs11_client->Pkcs11IsTpmTokenReady(
user_data_auth::Pkcs11IsTpmTokenReadyRequest(),
base::BindOnce(&CryptohomeWebUIHandler::OnPkcs11IsTpmTokenReady,
weak_ptr_factory_.GetWeakPtr()));
user_data_auth::GetAuthFactorExtendedInfoRequest req =
GenerateAuthFactorExtendedInfoRequest(kRecoveryIdHistoryDepth);
userdataauth_client->GetAuthFactorExtendedInfo(
req, base::BindOnce(&CryptohomeWebUIHandler::OnGetAuthFactorExtendedInfo,
weak_ptr_factory_.GetWeakPtr()));
auto ui_callback =
base::BindOnce(&CryptohomeWebUIHandler::GotIsTPMTokenEnabledOnUIThread,
weak_ptr_factory_.GetWeakPtr());
content::GetIOThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&crypto::IsTPMTokenEnabled,
base::BindOnce(&ForwardToUIThread,
std::move(ui_callback))));
}
void CryptohomeWebUIHandler::GotIsTPMTokenEnabledOnUIThread(
bool is_tpm_token_enabled) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::Value is_tpm_token_enabled_value(is_tpm_token_enabled);
SetCryptohomeProperty("is-tpm-token-ready",
std::move(is_tpm_token_enabled_value));
}
void CryptohomeWebUIHandler::OnGetTpmStatus(
const ::tpm_manager::GetTpmNonsensitiveStatusReply& reply) {
if (reply.status() != ::tpm_manager::STATUS_SUCCESS) {
LOG(ERROR) << "Failed to get TPM status; status: " << reply.status();
return;
}
// It also means TPM is ready if tpm manager reports TPM is owned.
SetCryptohomeProperty("tpm-is-ready", base::Value(reply.is_owned()));
SetCryptohomeProperty("tpm-is-enabled", base::Value(reply.is_enabled()));
SetCryptohomeProperty("tpm-is-owned", base::Value(reply.is_owned()));
SetCryptohomeProperty("has-reset-lock-permissions",
base::Value(reply.has_reset_lock_permissions()));
}
void CryptohomeWebUIHandler::OnGetAuthFactorExtendedInfo(
std::optional<user_data_auth::GetAuthFactorExtendedInfoReply> reply) {
std::string recovery_ids = "<empty>";
if (reply.has_value() &&
!reply->recovery_info_reply().recovery_ids().empty()) {
recovery_ids =
std::accumulate(reply->recovery_info_reply().recovery_ids().begin(),
reply->recovery_info_reply().recovery_ids().end(),
std::string(), [](std::string ss, std::string s) {
return ss.empty() ? s : ss + " " + s;
});
}
std::string recovery_seed = "<empty>";
if (reply.has_value() &&
!reply->recovery_info_reply().recovery_seed().empty()) {
recovery_seed = reply->recovery_info_reply().recovery_seed();
}
SetCryptohomeProperty("recovery_seed", base::Value(recovery_seed));
SetCryptohomeProperty("recovery_ids", base::Value(recovery_ids));
}
void CryptohomeWebUIHandler::OnIsMounted(
std::optional<user_data_auth::IsMountedReply> reply) {
bool mounted = false;
if (reply.has_value()) {
mounted = reply->is_mounted();
}
SetCryptohomeProperty("is-mounted", base::Value(mounted));
}
void CryptohomeWebUIHandler::OnPkcs11IsTpmTokenReady(
std::optional<user_data_auth::Pkcs11IsTpmTokenReadyReply> reply) {
bool ready = false;
if (reply.has_value()) {
ready = reply->ready();
}
SetCryptohomeProperty("pkcs11-is-tpm-token-ready", base::Value(ready));
}
void CryptohomeWebUIHandler::SetCryptohomeProperty(
const std::string& destination_id,
const base::Value& value) {
base::Value destination_id_value(destination_id);
web_ui()->CallJavascriptFunctionUnsafe("SetCryptohomeProperty",
destination_id_value, value);
}
} // namespace ash
|