keystore_keys_cryptographer.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2019 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/sync/nigori/keystore_keys_cryptographer.h"
  5. #include <utility>
  6. #include "base/check.h"
  7. #include "base/memory/ptr_util.h"
  8. #include "components/sync/engine/nigori/key_derivation_params.h"
  9. #include "components/sync/nigori/cryptographer_impl.h"
  10. #include "components/sync/protocol/encryption.pb.h"
  11. #include "components/sync/protocol/nigori_specifics.pb.h"
  12. namespace syncer {
  13. // static
  14. std::unique_ptr<KeystoreKeysCryptographer>
  15. KeystoreKeysCryptographer::CreateEmpty() {
  16. return base::WrapUnique(new KeystoreKeysCryptographer(
  17. CryptographerImpl::CreateEmpty(),
  18. /*keystore_keys=*/std::vector<std::string>()));
  19. }
  20. // static
  21. std::unique_ptr<KeystoreKeysCryptographer>
  22. KeystoreKeysCryptographer::FromKeystoreKeys(
  23. const std::vector<std::string>& keystore_keys) {
  24. if (keystore_keys.empty()) {
  25. return CreateEmpty();
  26. }
  27. std::unique_ptr<CryptographerImpl> cryptographer =
  28. CryptographerImpl::CreateEmpty();
  29. std::string last_key_name;
  30. for (const std::string& key : keystore_keys) {
  31. last_key_name =
  32. cryptographer->EmplaceKey(key, KeyDerivationParams::CreateForPbkdf2());
  33. // TODO(crbug.com/922900): possible behavioral change. Old implementation
  34. // fails only if we failed to add current keystore key. Failing to add any
  35. // of these keys doesn't seem valid. This line seems to be a good candidate
  36. // for UMA, as it's not a normal situation, if we fail to add any key.
  37. if (last_key_name.empty()) {
  38. return nullptr;
  39. }
  40. }
  41. DCHECK(!last_key_name.empty());
  42. cryptographer->SelectDefaultEncryptionKey(last_key_name);
  43. return base::WrapUnique(
  44. new KeystoreKeysCryptographer(std::move(cryptographer), keystore_keys));
  45. }
  46. KeystoreKeysCryptographer::KeystoreKeysCryptographer(
  47. std::unique_ptr<CryptographerImpl> cryptographer,
  48. const std::vector<std::string>& keystore_keys)
  49. : cryptographer_(std::move(cryptographer)), keystore_keys_(keystore_keys) {
  50. DCHECK(cryptographer_);
  51. }
  52. KeystoreKeysCryptographer::~KeystoreKeysCryptographer() = default;
  53. std::string KeystoreKeysCryptographer::GetLastKeystoreKeyName() const {
  54. return cryptographer_->GetDefaultEncryptionKeyName();
  55. }
  56. bool KeystoreKeysCryptographer::IsEmpty() const {
  57. return keystore_keys_.empty();
  58. }
  59. std::unique_ptr<KeystoreKeysCryptographer> KeystoreKeysCryptographer::Clone()
  60. const {
  61. return base::WrapUnique(
  62. new KeystoreKeysCryptographer(cryptographer_->Clone(), keystore_keys_));
  63. }
  64. std::unique_ptr<CryptographerImpl>
  65. KeystoreKeysCryptographer::ToCryptographerImpl() const {
  66. return cryptographer_->Clone();
  67. }
  68. bool KeystoreKeysCryptographer::EncryptKeystoreDecryptorToken(
  69. const sync_pb::NigoriKey& keystore_decryptor_key,
  70. sync_pb::EncryptedData* keystore_decryptor_token) const {
  71. DCHECK(keystore_decryptor_token);
  72. if (IsEmpty()) {
  73. return false;
  74. }
  75. return cryptographer_->EncryptString(
  76. keystore_decryptor_key.SerializeAsString(), keystore_decryptor_token);
  77. }
  78. bool KeystoreKeysCryptographer::DecryptKeystoreDecryptorToken(
  79. const sync_pb::EncryptedData& keystore_decryptor_token,
  80. sync_pb::NigoriKey* keystore_decryptor_key) const {
  81. return cryptographer_->Decrypt(keystore_decryptor_token,
  82. keystore_decryptor_key);
  83. }
  84. } // namespace syncer