fast_pair_data_encryptor_impl.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2021 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 "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor_impl.h"
  5. #include <array>
  6. #include <cstdint>
  7. #include "ash/quick_pair/common/fast_pair/fast_pair_metrics.h"
  8. #include "ash/quick_pair/common/logging.h"
  9. #include "ash/quick_pair/common/protocol.h"
  10. #include "ash/quick_pair/fast_pair_handshake/fast_pair_encryption.h"
  11. #include "ash/quick_pair/proto/fastpair.pb.h"
  12. #include "ash/quick_pair/repository/fast_pair/device_metadata.h"
  13. #include "ash/quick_pair/repository/fast_pair_repository.h"
  14. #include "ash/services/quick_pair/quick_pair_process.h"
  15. #include "base/check.h"
  16. #include "base/memory/ptr_util.h"
  17. #include "base/notreached.h"
  18. namespace ash {
  19. namespace quick_pair {
  20. namespace {
  21. // static
  22. FastPairDataEncryptorImpl::Factory* g_test_factory_ = nullptr;
  23. bool ValidateInputSize(const std::vector<uint8_t>& encrypted_bytes) {
  24. if (encrypted_bytes.size() != kBlockSizeBytes) {
  25. QP_LOG(WARNING) << __func__ << ": Encrypted bytes should have size = "
  26. << kBlockSizeBytes
  27. << ", actual = " << encrypted_bytes.size();
  28. return false;
  29. }
  30. return true;
  31. }
  32. } // namespace
  33. // static
  34. void FastPairDataEncryptorImpl::Factory::SetFactoryForTesting(
  35. Factory* g_test_factory) {
  36. g_test_factory_ = g_test_factory;
  37. }
  38. FastPairDataEncryptorImpl::Factory::~Factory() = default;
  39. // static
  40. void FastPairDataEncryptorImpl::Factory::CreateAsync(
  41. scoped_refptr<Device> device,
  42. base::OnceCallback<void(std::unique_ptr<FastPairDataEncryptor>)>
  43. on_get_instance_callback) {
  44. if (g_test_factory_) {
  45. g_test_factory_->CreateInstance(std::move(device),
  46. std::move(on_get_instance_callback));
  47. return;
  48. }
  49. if (device->protocol == Protocol::kFastPairInitial ||
  50. device->protocol == Protocol::kFastPairRetroactive) {
  51. CreateAsyncWithKeyExchange(std::move(device),
  52. std::move(on_get_instance_callback));
  53. } else if (device->protocol == Protocol::kFastPairSubsequent) {
  54. CreateAsyncWithAccountKey(std::move(device),
  55. std::move(on_get_instance_callback));
  56. } else {
  57. // This object doesn't handle any other protocols, calling with another
  58. // is a bug.
  59. NOTREACHED();
  60. }
  61. }
  62. // static
  63. void FastPairDataEncryptorImpl::Factory::CreateAsyncWithKeyExchange(
  64. scoped_refptr<Device> device,
  65. base::OnceCallback<void(std::unique_ptr<FastPairDataEncryptor>)>
  66. on_get_instance_callback) {
  67. QP_LOG(INFO) << __func__;
  68. // We first have to get the metadata in order to get the public key to use
  69. // to generate the new secret key pair.
  70. auto metadata_id = device->metadata_id;
  71. FastPairRepository::Get()->GetDeviceMetadata(
  72. metadata_id,
  73. base::BindOnce(
  74. &FastPairDataEncryptorImpl::Factory::DeviceMetadataRetrieved,
  75. std::move(device), std::move(on_get_instance_callback)));
  76. }
  77. // static
  78. void FastPairDataEncryptorImpl::Factory::CreateAsyncWithAccountKey(
  79. scoped_refptr<Device> device,
  80. base::OnceCallback<void(std::unique_ptr<FastPairDataEncryptor>)>
  81. on_get_instance_callback) {
  82. QP_LOG(INFO) << __func__;
  83. absl::optional<std::vector<uint8_t>> account_key =
  84. device->GetAdditionalData(Device::AdditionalDataType::kAccountKey);
  85. DCHECK(account_key);
  86. DCHECK_EQ(account_key->size(), static_cast<size_t>(kPrivateKeyByteSize));
  87. std::array<uint8_t, kPrivateKeyByteSize> private_key;
  88. std::copy_n(account_key->begin(), kPrivateKeyByteSize, private_key.begin());
  89. std::unique_ptr<FastPairDataEncryptorImpl> data_encryptor =
  90. base::WrapUnique(new FastPairDataEncryptorImpl(std::move(private_key)));
  91. std::move(on_get_instance_callback).Run(std::move(data_encryptor));
  92. }
  93. // static
  94. void FastPairDataEncryptorImpl::Factory::DeviceMetadataRetrieved(
  95. scoped_refptr<Device> device,
  96. base::OnceCallback<void(std::unique_ptr<FastPairDataEncryptor>)>
  97. on_get_instance_callback,
  98. DeviceMetadata* device_metadata,
  99. bool has_retryable_error) {
  100. if (!device_metadata) {
  101. QP_LOG(WARNING) << __func__ << ": No device metadata retrieved.";
  102. std::move(on_get_instance_callback).Run(nullptr);
  103. return;
  104. }
  105. const std::string& public_anti_spoofing_key =
  106. device_metadata->GetDetails().anti_spoofing_key_pair().public_key();
  107. absl::optional<fast_pair_encryption::KeyPair> key_pair =
  108. fast_pair_encryption::GenerateKeysWithEcdhKeyAgreement(
  109. public_anti_spoofing_key);
  110. RecordKeyPairGenerationResult(/*success=*/key_pair.has_value());
  111. if (key_pair) {
  112. std::unique_ptr<FastPairDataEncryptorImpl> data_encryptor =
  113. base::WrapUnique(new FastPairDataEncryptorImpl(key_pair.value()));
  114. std::move(on_get_instance_callback).Run(std::move(data_encryptor));
  115. } else {
  116. QP_LOG(WARNING) << __func__ << ": Failed to get key pair for device";
  117. std::move(on_get_instance_callback).Run(nullptr);
  118. }
  119. }
  120. FastPairDataEncryptorImpl::FastPairDataEncryptorImpl(
  121. const fast_pair_encryption::KeyPair& key_pair)
  122. : secret_key_(key_pair.private_key), public_key_(key_pair.public_key) {}
  123. FastPairDataEncryptorImpl::FastPairDataEncryptorImpl(
  124. const std::array<uint8_t, kPrivateKeyByteSize>& secret_key)
  125. : secret_key_(secret_key) {}
  126. FastPairDataEncryptorImpl::~FastPairDataEncryptorImpl() = default;
  127. const std::array<uint8_t, kBlockSizeBytes>
  128. FastPairDataEncryptorImpl::EncryptBytes(
  129. const std::array<uint8_t, kBlockSizeBytes>& bytes_to_encrypt) {
  130. return fast_pair_encryption::EncryptBytes(secret_key_, bytes_to_encrypt);
  131. }
  132. const absl::optional<std::array<uint8_t, kPublicKeyByteSize>>&
  133. FastPairDataEncryptorImpl::GetPublicKey() {
  134. return public_key_;
  135. }
  136. void FastPairDataEncryptorImpl::ParseDecryptedResponse(
  137. const std::vector<uint8_t>& encrypted_response_bytes,
  138. base::OnceCallback<void(const absl::optional<DecryptedResponse>&)>
  139. callback) {
  140. if (!ValidateInputSize(encrypted_response_bytes)) {
  141. std::move(callback).Run(absl::nullopt);
  142. return;
  143. }
  144. quick_pair_process::ParseDecryptedResponse(
  145. std::vector<uint8_t>(secret_key_.begin(), secret_key_.end()),
  146. encrypted_response_bytes, std::move(callback),
  147. base::BindOnce(
  148. &FastPairDataEncryptorImpl::QuickPairProcessStoppedOnResponse,
  149. weak_ptr_factory_.GetWeakPtr()));
  150. }
  151. void FastPairDataEncryptorImpl::ParseDecryptedPasskey(
  152. const std::vector<uint8_t>& encrypted_passkey_bytes,
  153. base::OnceCallback<void(const absl::optional<DecryptedPasskey>&)>
  154. callback) {
  155. if (!ValidateInputSize(encrypted_passkey_bytes)) {
  156. std::move(callback).Run(absl::nullopt);
  157. return;
  158. }
  159. quick_pair_process::ParseDecryptedPasskey(
  160. std::vector<uint8_t>(secret_key_.begin(), secret_key_.end()),
  161. encrypted_passkey_bytes, std::move(callback),
  162. base::BindOnce(
  163. &FastPairDataEncryptorImpl::QuickPairProcessStoppedOnPasskey,
  164. weak_ptr_factory_.GetWeakPtr()));
  165. }
  166. void FastPairDataEncryptorImpl::QuickPairProcessStoppedOnResponse(
  167. QuickPairProcessManager::ShutdownReason shutdown_reason) {
  168. QP_LOG(WARNING)
  169. << ": Quick Pair process stopped while decrypting response due to error: "
  170. << shutdown_reason;
  171. }
  172. void FastPairDataEncryptorImpl::QuickPairProcessStoppedOnPasskey(
  173. QuickPairProcessManager::ShutdownReason shutdown_reason) {
  174. QP_LOG(WARNING)
  175. << ": Quick Pair process stopped while decrypting passkey due to error: "
  176. << shutdown_reason;
  177. }
  178. } // namespace quick_pair
  179. } // namespace ash