trust_token_key_commitments_component_installer_policy.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2020 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. #include "components/component_updater/installer_policies/trust_token_key_commitments_component_installer_policy.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/bind.h"
  10. #include "base/command_line.h"
  11. #include "base/feature_list.h"
  12. #include "base/files/file_path.h"
  13. #include "base/files/file_util.h"
  14. #include "base/logging.h"
  15. #include "base/memory/ref_counted.h"
  16. #include "base/path_service.h"
  17. #include "base/task/thread_pool.h"
  18. #include "base/version.h"
  19. #include "components/component_updater/component_updater_paths.h"
  20. #include "components/component_updater/component_updater_switches.h"
  21. using component_updater::ComponentUpdateService;
  22. namespace {
  23. // The SHA256 of the SubjectPublicKeyInfo used to sign the extension.
  24. // The extension id is: kiabhabjdbkjdpjbpigfodbdjmbglcoo
  25. const uint8_t kTrustTokenKeyCommitmentsPublicKeySHA256[32] = {
  26. 0xa8, 0x01, 0x70, 0x19, 0x31, 0xa9, 0x3f, 0x91, 0xf8, 0x65, 0xe3,
  27. 0x13, 0x9c, 0x16, 0xb2, 0xee, 0xb4, 0xc7, 0xc2, 0x8e, 0xdb, 0x04,
  28. 0xd3, 0xaf, 0xeb, 0x07, 0x18, 0x15, 0x89, 0x23, 0x81, 0xad};
  29. const char kTrustTokenKeyCommitmentsManifestName[] =
  30. "Trust Token Key Commitments";
  31. // Attempts to load key commitments as raw JSON from their storage file,
  32. // returning the loaded commitments on success and nullopt on failure.
  33. absl::optional<std::string> LoadKeyCommitmentsFromDisk(
  34. const base::FilePath& path) {
  35. if (path.empty())
  36. return absl::nullopt;
  37. VLOG(1) << "Reading trust token key commitments from file: " << path.value();
  38. std::string ret;
  39. if (!base::ReadFileToString(path, &ret)) {
  40. VLOG(1) << "Failed reading from " << path.value();
  41. return absl::nullopt;
  42. }
  43. return ret;
  44. }
  45. } // namespace
  46. namespace component_updater {
  47. TrustTokenKeyCommitmentsComponentInstallerPolicy::
  48. TrustTokenKeyCommitmentsComponentInstallerPolicy(
  49. base::RepeatingCallback<void(const std::string&)> on_commitments_ready)
  50. : on_commitments_ready_(std::move(on_commitments_ready)) {}
  51. TrustTokenKeyCommitmentsComponentInstallerPolicy::
  52. ~TrustTokenKeyCommitmentsComponentInstallerPolicy() = default;
  53. bool TrustTokenKeyCommitmentsComponentInstallerPolicy::
  54. SupportsGroupPolicyEnabledComponentUpdates() const {
  55. return true;
  56. }
  57. bool TrustTokenKeyCommitmentsComponentInstallerPolicy::
  58. RequiresNetworkEncryption() const {
  59. // A network-path adversary being able to change the key commitments would
  60. // nullify the Trust Tokens protocol's privacy properties---but the component
  61. // updater guarantees integrity even if we return false here, and we don't
  62. // need confidentiality since this component's value is public and identical
  63. // for all users.
  64. return false;
  65. }
  66. update_client::CrxInstaller::Result
  67. TrustTokenKeyCommitmentsComponentInstallerPolicy::OnCustomInstall(
  68. const base::Value& manifest,
  69. const base::FilePath& install_dir) {
  70. return update_client::CrxInstaller::Result(0); // Nothing custom here.
  71. }
  72. void TrustTokenKeyCommitmentsComponentInstallerPolicy::OnCustomUninstall() {}
  73. base::FilePath
  74. TrustTokenKeyCommitmentsComponentInstallerPolicy::GetInstalledPath(
  75. const base::FilePath& base) {
  76. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  77. switches::kComponentUpdaterTrustTokensComponentPath)) {
  78. return base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
  79. switches::kComponentUpdaterTrustTokensComponentPath);
  80. }
  81. return base.Append(kTrustTokenKeyCommitmentsFileName);
  82. }
  83. void TrustTokenKeyCommitmentsComponentInstallerPolicy::ComponentReady(
  84. const base::Version& version,
  85. const base::FilePath& install_dir,
  86. base::Value manifest) {
  87. VLOG(1) << "Component ready, version " << version.GetString() << " in "
  88. << install_dir.value();
  89. LoadTrustTokensFromString(base::BindOnce(&LoadKeyCommitmentsFromDisk,
  90. GetInstalledPath(install_dir)),
  91. on_commitments_ready_);
  92. }
  93. // Called during startup and installation before ComponentReady().
  94. bool TrustTokenKeyCommitmentsComponentInstallerPolicy::VerifyInstallation(
  95. const base::Value& manifest,
  96. const base::FilePath& install_dir) const {
  97. // No need to actually validate the commitments here, since we'll do the
  98. // checking in NetworkService::SetTrustTokenKeyCommitments.
  99. return base::PathExists(GetInstalledPath(install_dir));
  100. }
  101. base::FilePath
  102. TrustTokenKeyCommitmentsComponentInstallerPolicy::GetRelativeInstallDir()
  103. const {
  104. return base::FilePath(FILE_PATH_LITERAL("TrustTokenKeyCommitments"));
  105. }
  106. void TrustTokenKeyCommitmentsComponentInstallerPolicy::GetHash(
  107. std::vector<uint8_t>* hash) const {
  108. GetPublicKeyHash(hash);
  109. }
  110. std::string TrustTokenKeyCommitmentsComponentInstallerPolicy::GetName() const {
  111. return kTrustTokenKeyCommitmentsManifestName;
  112. }
  113. update_client::InstallerAttributes
  114. TrustTokenKeyCommitmentsComponentInstallerPolicy::GetInstallerAttributes()
  115. const {
  116. return update_client::InstallerAttributes();
  117. }
  118. // static
  119. void TrustTokenKeyCommitmentsComponentInstallerPolicy::GetPublicKeyHash(
  120. std::vector<uint8_t>* hash) {
  121. DCHECK(hash);
  122. hash->assign(kTrustTokenKeyCommitmentsPublicKeySHA256,
  123. kTrustTokenKeyCommitmentsPublicKeySHA256 +
  124. std::size(kTrustTokenKeyCommitmentsPublicKeySHA256));
  125. }
  126. // static
  127. void TrustTokenKeyCommitmentsComponentInstallerPolicy::
  128. LoadTrustTokensFromString(
  129. base::OnceCallback<absl::optional<std::string>()> load_keys_from_disk,
  130. base::OnceCallback<void(const std::string&)> on_commitments_ready) {
  131. base::ThreadPool::PostTaskAndReplyWithResult(
  132. FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
  133. std::move(load_keys_from_disk),
  134. base::BindOnce(
  135. // Only bother sending commitments to the network service if we loaded
  136. // them successfully.
  137. [](base::OnceCallback<void(const std::string&)> on_commitments_ready,
  138. absl::optional<std::string> loaded_commitments) {
  139. if (loaded_commitments.has_value()) {
  140. std::move(on_commitments_ready).Run(loaded_commitments.value());
  141. }
  142. },
  143. std::move(on_commitments_ready)));
  144. }
  145. } // namespace component_updater