Skip to content

Commit

Permalink
Migrate CryptohomeClient::TpmTokenInfo and chromeos::TPMTokenInfo.
Browse files Browse the repository at this point in the history
BUG=None
TEST=Ran trybot.

Change-Id: Ida37b6128db93745f3ea1c33f63e1531509cee4c
Reviewed-on: https://chromium-review.googlesource.com/694763
Reviewed-by: Matt Menke <mmenke@chromium.org>
Reviewed-by: Steven Bennetts <stevenjb@chromium.org>
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#506114}
  • Loading branch information
Hidehiko Abe authored and Commit Bot committed Oct 3, 2017
1 parent 68ea4d8 commit 2c6b79e
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 193 deletions.
22 changes: 10 additions & 12 deletions chrome/browser/profiles/profile_io_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/net/nss_context.h"
#include "chromeos/dbus/cryptohome_client.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/settings/cros_settings_names.h"
#include "chromeos/tpm/tpm_token_info_getter.h"
Expand Down Expand Up @@ -283,16 +284,14 @@ class DebugDevToolsInterceptor : public net::URLRequestInterceptor {
void DidGetTPMInfoForUserOnUIThread(
std::unique_ptr<chromeos::TPMTokenInfoGetter> getter,
const std::string& username_hash,
const chromeos::TPMTokenInfo& info) {
base::Optional<chromeos::CryptohomeClient::TpmTokenInfo> token_info) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (info.tpm_is_enabled && info.token_slot_id != -1) {
if (token_info.has_value() && token_info->slot != -1) {
DVLOG(1) << "Got TPM slot for " << username_hash << ": "
<< info.token_slot_id;
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(&crypto::InitializeTPMForChromeOSUser,
username_hash, info.token_slot_id));
<< token_info->slot;
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&crypto::InitializeTPMForChromeOSUser,
username_hash, token_info->slot));
} else {
NOTREACHED() << "TPMTokenInfoGetter reported invalid token.";
}
Expand All @@ -314,10 +313,9 @@ void GetTPMInfoForUserOnUIThread(const AccountId& account_id,
// before TPM token info is fetched.
// TODO(tbarzic, pneubeck): Handle this in a nicer way when this logic is
// moved to a separate profile service.
token_info_getter->Start(
base::Bind(&DidGetTPMInfoForUserOnUIThread,
base::Passed(&scoped_token_info_getter),
username_hash));
token_info_getter->Start(base::BindOnce(&DidGetTPMInfoForUserOnUIThread,
std::move(scoped_token_info_getter),
username_hash));
}

void StartTPMSlotInitializationOnIOThread(const AccountId& account_id,
Expand Down
3 changes: 3 additions & 0 deletions chromeos/dbus/cryptohome_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class CHROMEOS_EXPORT CryptohomeClient : public DBusClient {
uint64_t total)>
DircryptoMigrationProgessHandler;

// TPM Token Information retrieved from cryptohome.
// For invalid token |label| and |user_pin| will be empty, while |slot| will
// be set to -1.
struct TpmTokenInfo {
// Holds the PKCS #11 token label. This is not useful in practice to
// identify a token but may be meaningful to a user.
Expand Down
24 changes: 6 additions & 18 deletions chromeos/tpm/tpm_token_info_getter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <stdint.h>

#include <utility>

#include "base/bind.h"
#include "base/location.h"
#include "base/task_runner.h"
Expand Down Expand Up @@ -36,13 +38,6 @@ base::TimeDelta GetNextRequestDelayMs(base::TimeDelta last_delay) {

namespace chromeos {

TPMTokenInfo::TPMTokenInfo()
: tpm_is_enabled(false),
token_slot_id(-1) {
}

TPMTokenInfo::~TPMTokenInfo() {}

// static
std::unique_ptr<TPMTokenInfoGetter> TPMTokenInfoGetter::CreateForUserToken(
const AccountId& account_id,
Expand All @@ -63,11 +58,11 @@ std::unique_ptr<TPMTokenInfoGetter> TPMTokenInfoGetter::CreateForSystemToken(

TPMTokenInfoGetter::~TPMTokenInfoGetter() {}

void TPMTokenInfoGetter::Start(const TPMTokenInfoCallback& callback) {
void TPMTokenInfoGetter::Start(TpmTokenInfoCallback callback) {
CHECK(state_ == STATE_INITIAL);
CHECK(!callback.is_null());

callback_ = callback;
callback_ = std::move(callback);

state_ = STATE_STARTED;
Continue();
Expand Down Expand Up @@ -130,7 +125,7 @@ void TPMTokenInfoGetter::OnTpmIsEnabled(base::Optional<bool> tpm_is_enabled) {

if (!tpm_is_enabled.value()) {
state_ = STATE_DONE;
callback_.Run(TPMTokenInfo());
std::move(callback_).Run(base::nullopt);
return;
}

Expand All @@ -146,14 +141,7 @@ void TPMTokenInfoGetter::OnPkcs11GetTpmTokenInfo(
}

state_ = STATE_DONE;

TPMTokenInfo out_token_info;
out_token_info.tpm_is_enabled = true;
out_token_info.token_name = token_info->label;
out_token_info.user_pin = token_info->user_pin;
out_token_info.token_slot_id = token_info->slot;

callback_.Run(out_token_info);
std::move(callback_).Run(std::move(token_info));
}

} // namespace chromeos
23 changes: 4 additions & 19 deletions chromeos/tpm/tpm_token_info_getter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,12 @@ class TaskRunner;

namespace chromeos {

// Information retrieved from cryptohome by TPMTokenInfoGetter.
// For invalid token |token_name| and |user_pin| will be empty, while
// |token_slot_id| will be set to -1.
// TODO(hidehiko): This struct is conceptually as same as
// base::Optional<CryptohomeClient::TpmTokenInfo>. Migrate into it.
struct TPMTokenInfo {
// Default constructor creates token info for disabled TPM.
TPMTokenInfo();
~TPMTokenInfo();

bool tpm_is_enabled;
std::string token_name;
std::string user_pin;
int token_slot_id;
};

// Class for getting a user or the system TPM token info from cryptohome during
// TPM token loading.
class CHROMEOS_EXPORT TPMTokenInfoGetter {
public:
using TPMTokenInfoCallback = base::Callback<void(const TPMTokenInfo& info)>;
using TpmTokenInfoCallback = base::OnceCallback<void(
base::Optional<CryptohomeClient::TpmTokenInfo> token_info)>;

// Factory method for TPMTokenInfoGetter for a user token.
static std::unique_ptr<TPMTokenInfoGetter> CreateForUserToken(
Expand All @@ -64,7 +49,7 @@ class CHROMEOS_EXPORT TPMTokenInfoGetter {
// The object may get deleted before |callback| is called, which is equivalent
// to cancelling the info getting (in which case |callback| will never get
// called).
void Start(const TPMTokenInfoCallback& callback);
void Start(TpmTokenInfoCallback callback);

private:
enum Type {
Expand Down Expand Up @@ -109,7 +94,7 @@ class CHROMEOS_EXPORT TPMTokenInfoGetter {
// token.
AccountId account_id_;

TPMTokenInfoCallback callback_;
TpmTokenInfoCallback callback_;

// The current request delay before the next attempt to initialize the
// TPM. Will be adapted after each attempt.
Expand Down
Loading

0 comments on commit 2c6b79e

Please sign in to comment.